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.

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);
        });
    });
});