I’ve been searching all over and can’t figure out why this isn’t working.
I’m trying to test a very basic ajax action. Here are 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 I’d get to the commit method, but the page would reload, which defeats the point of remote=>true
Also the commit.js never seemed to get called.
Note 1: If I exclude this line, I get the blank page to /commit. Including it makes the page just reload
Note 2: I’ve tried both of these approaches suggested by other SO posts
Note 3: I’ve tried both using link_to and form_tag
Can anyone help? Thanks!
Why did you put there 2 lines?
Remove the second one!
Replace:
with:
The same with the form:
Make your:
as:
Note:
POSTis the default behavior, you can omit it fromform_tag.