weixin_33697898 2018-11-15 22:46 采纳率: 0%
浏览 24

Rails中的Ajax请求

Hello I am having a problem while trying to do an Ajax request, I think is entering to a wrong route, this is my js.

$(function () {
    $('#sucursal').change(function () {
        $.ajax({
            url: "/tickets/filter_by_id",
            type: "GET",
            data: { sucursal: $('#sucursal option:selected').text() }
        }).done(function (msg) {
            console.log(msg)
        }).fail(function (msg, txtStatus) {
            console.log(msg);
        });
    });
});

and this is my controller.

def filter_by_sucursal
      render :text => "Ok"
end

and throws this:

ActiveRecord::RecordNotFound in TicketsController#show

Couldn't find Ticket with 'id'=filter_by_id

I think it is pointing there because of this but I am not sure.

before_action :set_ticket, only: [:show, :edit, :update, :destroy]

Hope you could help me. Greetings.

  • 写回答

1条回答 默认 最新

  • DragonWar% 2018-11-15 23:08
    关注

    You need to keep in mind that your routes are searched from top to bottom. So, when you do this:

    resources :tickets 
    post '/tickets/filter', to: 'tickets#filter_by',          as: 'filter_by' 
    get '/tickets/filter',  to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' 
    

    You get:

                tickets GET    /tickets(.:format)                        tickets#index
                        POST   /tickets(.:format)                        tickets#create
             new_ticket GET    /tickets/new(.:format)                    tickets#new
            edit_ticket GET    /tickets/:id/edit(.:format)               tickets#edit
                 ticket GET    /tickets/:id(.:format)                    tickets#show
                        PATCH  /tickets/:id(.:format)                    tickets#update
                        PUT    /tickets/:id(.:format)                    tickets#update
                        DELETE /tickets/:id(.:format)                    tickets#destroy
              filter_by POST   /tickets/filter(.:format)                 tickets#filter_by
     filter_by_sucursal GET    /tickets/filter(.:format)                 tickets#filter_by_sucursal
    

    And, as you can see, GET /tickets/:id comes before GET /tickets/filter. So, anything that looks like GET /tickets/... is going to route to tickets#show.

    Your routes.rb should look more like:

    Rails.application.routes.draw do 
      resources :tickets do 
        collection do 
          post :filter, as: :filter_by
          get  :filter, as: :filter_by_sucursal
        end
      end
    end
    

    This will give you:

              filter_by_tickets POST   /tickets/filter(.:format)         tickets#filter_by
     filter_by_sucursal_tickets GET    /tickets/filter(.:format)         tickets#filter_by_sucursal
                        tickets GET    /tickets(.:format)                tickets#index
                                POST   /tickets(.:format)                tickets#create
                     new_ticket GET    /tickets/new(.:format)            tickets#new
                    edit_ticket GET    /tickets/:id/edit(.:format)       tickets#edit
                         ticket GET    /tickets/:id(.:format)            tickets#show
                                PATCH  /tickets/:id(.:format)            tickets#update
                                PUT    /tickets/:id(.:format)            tickets#update
                                DELETE /tickets/:id(.:format)            tickets#destroy
    

    Alternatively, you could do:

    resources :tickets do 
      collection do 
        post :filter, to: 'tickets#filter_by', as: :filter_by
      end
      member do 
        get  :filter, to: 'tickets#filter_by_sucursal', as: :filter_by_sucursal
      end
    end
    

    Which would give you:

            filter_by_tickets POST   /tickets/filter(.:format)           tickets#filter_by
    filter_by_sucursal_ticket GET    /tickets/:id/filter(.:format)       tickets#filter_by_sucursal
                      tickets GET    /tickets(.:format)                  tickets#index
                              POST   /tickets(.:format)                  tickets#create
                   new_ticket GET    /tickets/new(.:format)              tickets#new
                  edit_ticket GET    /tickets/:id/edit(.:format)         tickets#edit
                       ticket GET    /tickets/:id(.:format)              tickets#show
                              PATCH  /tickets/:id(.:format)              tickets#update
                              PUT    /tickets/:id(.:format)              tickets#update
                              DELETE /tickets/:id(.:format)              tickets#destroy
    

    Then in your javascript you could do:

    $(function () {
        $('#sucursal').change(function () {
            $.ajax({
                url: "/tickets/#{$('#sucursal option:selected').text()}/filter",
                type: "GET",
            }).done(function (msg) {
                console.log(msg)
            }).fail(function (msg, txtStatus) {
                console.log(msg);
            });
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测