Rails 3.2.x remote => true still reloads the page

I searched everything and cannot understand why this is not working.

I am trying to test a very simple ajax action. Here is my code:

Controller:

def commit respond_to do |format| format.html { redirect_to :action => "index" } # see note 1 format.js { render :layout => false } # see note 2 format.js { render :nothing => true } end end 

View:

 <%= link_to "commit", :action => "commit", :remote => true %> <%= form_tag( :action => "commit", :remote => true, :method => :post) do %> <%= submit_tag "commit" %> <% end %> <div id='message'></div> 

commit.js.erb

 console.log('committed'); $('#message').html("committed"); 

The problem is that I will go to the commit method, but the page will reload, which will cause the remote => true point to fail. Also commit.js was never called.

Note 1: If I exclude this line, I get a blank / commit page. Turning it on, the page just reloads
Note 2: I tried both of these approaches suggested by other SO posts Note 3: I tried using link_to and form_tag links

Can anyone help? Thanks!

+4
source share
1 answer

Why did you put 2 lines there?

  format.js { render :layout => false } # see note 2 format.js { render :nothing => true } 

Remove the second one!

Replace:

 <%= link_to "commit", :action => "commit", :remote => true %> 

from:

 <%= link_to "commit", commit_path, :remote => true %> 

<h / "> Same with form:

Make your own:

 <%= form_tag( :action => "commit", :remote => true, :method => :post) do %> 

as:

 <%= form_tag(commit_path, :remote => true) do %> 

Note. POST is the default behavior, you can omit it from form_tag .

+4
source

Source: https://habr.com/ru/post/1411743/


All Articles