Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ':remote => true' works in rails

Tags:

I don't understand how :remote => true works in rails. I know that when I write it an ajax request is sent and the .js response is made. But how does this work? I mean, where does the action go, etc.?

like image 532
asdfkjasdfjk Avatar asked Dec 23 '13 04:12

asdfkjasdfjk


People also ask

What is Rails Ujs?

Rails UJS (Unobtrusive JavaScript) is the JavaScript library that helps Rails do its magic when we use options like remote: true for many of the html helpers. In this article I'll try to explain the main concept of how this works to make it transparent for the user.

Can you use JavaScript with Ruby on Rails?

Rails uses a technique called "Unobtrusive JavaScript" to handle attaching JavaScript to the DOM. This is generally considered to be a best-practice within the frontend community, but you may occasionally read tutorials that demonstrate other ways.

How does remote true work with the rails remote helper?

The Rails documentation for remote: true is great at explaining how to add the helper to your views. It does not, however, fully explain how remote: true works in tandem with a controller and a Javascript file to generate and manage JSON responses to the ajax call.

How does remote true work with Ajax?

It does not, however, fully explain how remote: true works in tandem with a controller and a Javascript file to generate and manage JSON responses to the ajax call. AJAX involves a clientside (browser) request to your server that generates a response back to the client that must be handled.

How do I use AJAX calls in rails?

AJAX involves a clientside (browser) request to your server that generates a response back to the client that must be handled. Below is a short tutorial on how to craft the full circuit of requesting and responding via remote: true ajax calls in Rails. You can add the Rails helper remote: true to any other Rails helper that generates a web request.

How to submit a form using Ajax in rails?

If you want to submit a form using AJAX you can do this without having to write an AJAX request. Rails can handle it for you. How? Use the remote: true option with form_for. Now when you click “Create” Rails will send an AJAX request for you & the page won’t reload.


1 Answers

Let me explain the whole flow of AJAX-Rails and remote=> true.

First, when you add remote => true to the form it will submit or call the action which you have defined in the form.

Here is an example:

<%= form_tag({:controller => 'my', :action => 'my_data'},:id => 'filter_form', :remote => true) do %>  #code here <%= submit_tag 'save', :name => 'commit'%> <%end%> 

Now, the above code will go to my_data action in my controller.

Here you can define the response type with:

def my_data   #actions on data here   respond_to do |format|     format.js   end end 

Now you have to make a ".js"" file with the same name as the action:

my_data.js.erb 

This ".js" file will handle the form. You could write and update document elements through jQuery and JavaScript.

like image 168
Bharat soni Avatar answered Sep 22 '22 16:09

Bharat soni