Ajax request in rails












0














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#shownnCouldn'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.










share|improve this question


















  • 1




    The problem is with how you declare your routes in routes.rb. The ajax call is resolving to get tickets/:id and filtered_by_id is being interpreted as :id which is why you're getting the RecordNotFound error. Please add routes.rb to your question.
    – jvillian
    Nov 15 at 22:54










  • this is my routes.rb Rails.application.routes.draw do resources :tickets post '/tickets/filter', to: 'tickets#filter_by', as: 'filter_by' get '/tickets/filter', to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' end
    – Mexa Fireg
    Nov 15 at 22:56






  • 1




    In you routes: resources :tickets do get :filter_by_id, on: :collection end
    – edudepetris
    Nov 15 at 22:56










  • You should really edit your question to include the code. It's so hard to read in comments.
    – jvillian
    Nov 15 at 23:00










  • I am sorry I've never commented before. Unknown actionnnThe action 'filter_by_id' could not be found for TicketsControllern. this is what I am getting now still wanting an id :(.
    – Mexa Fireg
    Nov 15 at 23:03


















0














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#shownnCouldn'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.










share|improve this question


















  • 1




    The problem is with how you declare your routes in routes.rb. The ajax call is resolving to get tickets/:id and filtered_by_id is being interpreted as :id which is why you're getting the RecordNotFound error. Please add routes.rb to your question.
    – jvillian
    Nov 15 at 22:54










  • this is my routes.rb Rails.application.routes.draw do resources :tickets post '/tickets/filter', to: 'tickets#filter_by', as: 'filter_by' get '/tickets/filter', to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' end
    – Mexa Fireg
    Nov 15 at 22:56






  • 1




    In you routes: resources :tickets do get :filter_by_id, on: :collection end
    – edudepetris
    Nov 15 at 22:56










  • You should really edit your question to include the code. It's so hard to read in comments.
    – jvillian
    Nov 15 at 23:00










  • I am sorry I've never commented before. Unknown actionnnThe action 'filter_by_id' could not be found for TicketsControllern. this is what I am getting now still wanting an id :(.
    – Mexa Fireg
    Nov 15 at 23:03
















0












0








0







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#shownnCouldn'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.










share|improve this question













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#shownnCouldn'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.







javascript ruby-on-rails ajax model-view-controller ruby-on-rails-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 at 22:46









Mexa Fireg

287




287








  • 1




    The problem is with how you declare your routes in routes.rb. The ajax call is resolving to get tickets/:id and filtered_by_id is being interpreted as :id which is why you're getting the RecordNotFound error. Please add routes.rb to your question.
    – jvillian
    Nov 15 at 22:54










  • this is my routes.rb Rails.application.routes.draw do resources :tickets post '/tickets/filter', to: 'tickets#filter_by', as: 'filter_by' get '/tickets/filter', to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' end
    – Mexa Fireg
    Nov 15 at 22:56






  • 1




    In you routes: resources :tickets do get :filter_by_id, on: :collection end
    – edudepetris
    Nov 15 at 22:56










  • You should really edit your question to include the code. It's so hard to read in comments.
    – jvillian
    Nov 15 at 23:00










  • I am sorry I've never commented before. Unknown actionnnThe action 'filter_by_id' could not be found for TicketsControllern. this is what I am getting now still wanting an id :(.
    – Mexa Fireg
    Nov 15 at 23:03
















  • 1




    The problem is with how you declare your routes in routes.rb. The ajax call is resolving to get tickets/:id and filtered_by_id is being interpreted as :id which is why you're getting the RecordNotFound error. Please add routes.rb to your question.
    – jvillian
    Nov 15 at 22:54










  • this is my routes.rb Rails.application.routes.draw do resources :tickets post '/tickets/filter', to: 'tickets#filter_by', as: 'filter_by' get '/tickets/filter', to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' end
    – Mexa Fireg
    Nov 15 at 22:56






  • 1




    In you routes: resources :tickets do get :filter_by_id, on: :collection end
    – edudepetris
    Nov 15 at 22:56










  • You should really edit your question to include the code. It's so hard to read in comments.
    – jvillian
    Nov 15 at 23:00










  • I am sorry I've never commented before. Unknown actionnnThe action 'filter_by_id' could not be found for TicketsControllern. this is what I am getting now still wanting an id :(.
    – Mexa Fireg
    Nov 15 at 23:03










1




1




The problem is with how you declare your routes in routes.rb. The ajax call is resolving to get tickets/:id and filtered_by_id is being interpreted as :id which is why you're getting the RecordNotFound error. Please add routes.rb to your question.
– jvillian
Nov 15 at 22:54




The problem is with how you declare your routes in routes.rb. The ajax call is resolving to get tickets/:id and filtered_by_id is being interpreted as :id which is why you're getting the RecordNotFound error. Please add routes.rb to your question.
– jvillian
Nov 15 at 22:54












this is my routes.rb Rails.application.routes.draw do resources :tickets post '/tickets/filter', to: 'tickets#filter_by', as: 'filter_by' get '/tickets/filter', to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' end
– Mexa Fireg
Nov 15 at 22:56




this is my routes.rb Rails.application.routes.draw do resources :tickets post '/tickets/filter', to: 'tickets#filter_by', as: 'filter_by' get '/tickets/filter', to: 'tickets#filter_by_sucursal', as: 'filter_by_sucursal' end
– Mexa Fireg
Nov 15 at 22:56




1




1




In you routes: resources :tickets do get :filter_by_id, on: :collection end
– edudepetris
Nov 15 at 22:56




In you routes: resources :tickets do get :filter_by_id, on: :collection end
– edudepetris
Nov 15 at 22:56












You should really edit your question to include the code. It's so hard to read in comments.
– jvillian
Nov 15 at 23:00




You should really edit your question to include the code. It's so hard to read in comments.
– jvillian
Nov 15 at 23:00












I am sorry I've never commented before. Unknown actionnnThe action 'filter_by_id' could not be found for TicketsControllern. this is what I am getting now still wanting an id :(.
– Mexa Fireg
Nov 15 at 23:03






I am sorry I've never commented before. Unknown actionnnThe action 'filter_by_id' could not be found for TicketsControllern. this is what I am getting now still wanting an id :(.
– Mexa Fireg
Nov 15 at 23:03














1 Answer
1






active

oldest

votes


















0














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





share|improve this answer























  • Thank you so much for your time, excellent answer.
    – Mexa Fireg
    Nov 15 at 23:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328937%2fajax-request-in-rails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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





share|improve this answer























  • Thank you so much for your time, excellent answer.
    – Mexa Fireg
    Nov 15 at 23:21
















0














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





share|improve this answer























  • Thank you so much for your time, excellent answer.
    – Mexa Fireg
    Nov 15 at 23:21














0












0








0






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 at 23:20

























answered Nov 15 at 23:08









jvillian

12.2k52135




12.2k52135












  • Thank you so much for your time, excellent answer.
    – Mexa Fireg
    Nov 15 at 23:21


















  • Thank you so much for your time, excellent answer.
    – Mexa Fireg
    Nov 15 at 23:21
















Thank you so much for your time, excellent answer.
– Mexa Fireg
Nov 15 at 23:21




Thank you so much for your time, excellent answer.
– Mexa Fireg
Nov 15 at 23:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328937%2fajax-request-in-rails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents