Search code examples
ruby-on-railsrubyajaxruby-on-rails-4link-to

Rails: Can I apply Ajax to a link_to method?


As I understand it, Ajax allows you to only refresh the part of the page that you specify, as opposed to reloading the whole thing. Is there a way of doing this with a link_to method, for example, <%= link_to 'approve', approve_model_path(@model.id), method: :put %> ? I can't figure out how a link_to could be executed without the whole page being reloaded.


Solution

  • You can apply ajax by setting remote to true:

    <%= link_to 'approve', approve_model_path(@model.id), method: :put, remote: true %>
    

    This does a js request, then you need an approve.js.erb to do the few changes you need (without refreshing the page)