In the past, when I wanted to update part of my view through Ajax, I did the following:
- create the partial part of the part that I want to update and give it a unique identifier, say
#tracks - create a special action in the controller for this Ajax call, say
remove_track , which updates all values, etc. and adds format.js - create a new JS file with the same name as the action, so Rails will automatically call it
remove_track.js.erb , which contains something like: $('#tracks').html("<%=j render 'cds/show_tracks' %>"); - set
remote: true to the link that invokes this action.
All this is fine, but now I'm trying to remove and update the general index view using the usual destroy method for flexibility, that is, I can call this method either through Ajax, or usually. I thought that such a common thing, that there should be a better way than all of the above.
I can get the destroy method to call my destroy.js.erb file by simply placing it in the controller:
format.js { layout: false }
and of course set remote: true in the link.
what i cannot do is get a view for the update. The table I want to update is enclosed in a div with a unique identifier, but since it is not partial, it refuses to update the contents. Maybe I missed something.
Am I doomed to create a partial and update it using the method described above, or is there a more magical way to execute it (other than using Turbolinks)?
Thanks.
PS In addition, I just noticed that this has an additional drawback: I can not pass the remaining parameters to the destroy method, since it only passes the identifier of the object in order to destroy it using regular CRUD routes. If I try to use platform(action: destroy) or platform(method: delete) , I get an error:
No route matches {:action=>"destroy", :controller=>"platforms"}
Which means that I need to create a new route if I want to pass these parameters ...
Another disadvantage of all this is that I repeat all the search and sort logic that I have in the index method again in the destroy method. I am sure this is definitely not a way to do this.
kakubei
source share