Alex Rivera | Logout

Rails 3 submit form with link

Asked 2011-01-25T08:59:59.887
16

How I can submit form with link on correct rails 3 format? Thanks.

<%= form_for @post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>

My code sample.

Edit
Report

1 Answer

5

In Rails 3, the link_to_remote helper is gone, but it's replaced with

link_to 'submit me', url_for(@post), {:remote => true, :class => 'submit_me'}

In your case, you likely want your form to do the AJAX, like so:

<%= form_for @post, :remote => true do |f| %>
  <%= f.label :title %><br>
  <%= f.text_field :title %>
  <p><%= f.submit %></p>
<% end %>

With a companion link:

link_to 'submit me', '#', :class => 'submit_me'

Then, in an .js file included in the page body:

$('.submit_me').click(function() {
  $('form').submit();
  return false;
});

The idea is that anything more complicated than turning a link or form into an ajax request should be done with the jQuery callbacks, as listed here:

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

And if you want to really get into interactive AJAX requests, go here for a great 2-part article on it.

answered 2011-06-03T14:35:46.970

Your Answer