1

I'm pretty new to Ruby on Rails and having a hard time using Ajax calls in rails and I was hoping someone could help me out.

$.ajax({
            url: 'organizations/set_all_priorities',
            data: {ticket1: ticket1, ticket2: ticket2, ticket3: ticket3}, 
            method: "PUT" 
        });

This is my Ajax call. I have 3 ticket vars containing an integer. The method calls my organization controller's method set_all_priorities.

The set_all_priorities method in my organizations_controller looks like this:

  def set_all_priorities
  puts "I am now in set_all_priorities"

  org = Organization.find_by(:zendesk_organization_id => Ticket.find_by(:ticket_id => params[:ticket1]).organization_id)
  org.update_all(:priority1 => params[:ticket1])
  org.update_all(:priority2 => params[:ticket2])
  org.update_all(:priority3 => params[:ticket3])
  org.save
end

end

Unfortunately it is not even calling the method as I cannot find my puts command anywhere when calling my Ajax function.

Finally, in my routes I have the following line:

  put 'organizations/set_all_priorities', :to => 'organizations#set_all_priorities'

The error I am getting is this: ActiveRecord::RecordNotFound (Couldn't find Organization with 'id'=set_all_priorities):

It seems like my update method is being called, but seeing as I have defined my own method in the controller and it's not being called that makes no sense to me.

If anybody knows the solution to this that would be awesome. Thanks everyone!

2
  • To check this error, you should define this ajax call in javascript function and then call this function as their name and let me know what is issue you are facing on browser for this ajax call. Commented Oct 6, 2015 at 12:48
  • @dipakgupta i have defined it in function. It is not giving me an error, however, the method simply doesn't seem to call the set_all_priorities method in my organizations_controller. The changes arent made and the puts cannot be found anywhere. Commented Oct 6, 2015 at 12:54

3 Answers 3

2

Please try like this:

$.ajax({
  url: '/organizations/set_all_priorities',
  type: 'PUT',
  data: {ticket1: ticket1, ticket2: ticket2, ticket3: ticket3}
});
Sign up to request clarification or add additional context in comments.

3 Comments

I changed it to what you suggested but unfortunately to no avail. I'm still getting the same error: ActiveRecord::RecordNotFound (Couldn't find Organization with 'id'=set_all_priorities): It's for some reason still calling my update method or something expecting a params[:id]
Try with this: Organization.where(:id => Ticket.where(:id => params[:ticket1])[0].organization_id)
It doesn't even manage to call the set_all_priorities method so changing that piece of code has no effect at all I'm afraid.
1

Based on that error, it looks like Rails is directing calls to that endpoint to the #show method in the controller. The most likely cause for this is that the custom endpoint you're defining is placed after resources :organizations.

Per the Rails documentation, routes are interpreted in the order in which they appear in the routes file, meaning your custom endpoint will need to appear before resources :organizations, or another endpoint which accepts a parameter.

put 'organizations/set_all_priorities', :to => 'organizations#set_all_priorities'
resources :organizations

See the second additional note under section 2.2.

http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

Comments

0

Probably organization should be in plural everywhere:

$.ajax({
        url: 'organizations/set_all_priorities',
        data: {ticket1: ticket1, ticket2: ticket2, ticket3: ticket3}, 
        method: "PUT" 
      });

And

 put 'organizations/set_all_priorities', :to => 'organizations#set_all_priorities'

At least rails generators creates controller and views in plural (app/controllers/organizations_controller.rb or /app/views/organizations/index.html.erb) and model in singular (app/models/organization.rb)

4 Comments

My bad, due to changing the file so many times I had indeed made some errors there. I just replaced it all with plural words but now I get the following error: ActiveRecord::RecordNotFound (Couldn't find Organization with 'id'=set_all_priorities): Seems to be calling the update method in my controller but that makes no sense because I have defined my own method and am trying to call it.
it means that routes, ajax (sending query) and controller works, and now You must correct set_all_priorities method or ajax query data
Yeah but how do i do that exactly? I have my data field in the ajax call that passes 3 integers. What do I need to adapt in my set_all_priorities method for it to work? As far as I know Im supposed to get my data out of params[] in the method itself.
Split Organization.find_by(:zendesk_organization_id => Ticket.find_by(:ticket_id => params[:ticket1]).organization_id) to multiple rows (eg ticket_id = params[:ticket1]).organization_id, org_id = Ticket.find_by ticket_id), org = Organization.find_by(:zendesk_organization_id => org_id```) and look parameters and result values in each row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.