Lamp
Lamp

Reputation: 1084

Ajax remote form in Rails 3

In my Rails 3 app I have a simple form like:

<%= form_tag({:controller => "activity", :action => "record"}, :method => "post", :remote => true, :class => "myform") do  %>
    <%= hidden_field_tag 'name', 'Jack' %>
<% end %>

my controller: (activity_controller.rb)

def record
 #...something
 respond_to |format|
   format.js          #   app/views/activity/record.js.erb
 end
end

In record.js.erb:

alert("hello");

When I'm trying to submit the form with jquery, from firebug console with:

$('.myform').submit();

the form makes a post request normally, but controller does not render record.js.

Redirects to localhost/activity/record.

I suppose that something wrong goes with my form..

Can you identify the problem?

Upvotes: 0

Views: 356

Answers (2)

Hugo
Hugo

Reputation: 2129

If you use Rails > 3 make sure you have "jquery_ujs.js" which replaces "rails.js"

https://github.com/rails/jquery-ujs

Upvotes: 1

DanneManne
DanneManne

Reputation: 21180

Make sure that you have included the rails.js and the csrf_meta_tag helper in your layout. It is a very common mistake.

Upvotes: 0

Related Questions