Performing AJAX Calls in Rails Code Example

Last Updated On Saturday 4th Dec 2021

In Rails, there are so many view helper methods written in Ruby to generate HTML.

  • It is also possible to add AJAX to the generated HTML elements using the AJAX helpers provided in Rails.
  • This is achieved with the Rails helper remote: true.

Making the ajax request with remote: true helper in the View

	<%= link_to admin_message_path(msg.id), method: 'put', remote: true do %>
  <span id=<%= "button-text-#{msg.id}" %>><%= display_text(msg) %></span>
<% end %>
	
  • The Rails helper remote: true can be added to any other Rails view helper that generates a web request.
  • It will stop the default action like preventDefault().