Monday, April, 21, 2008

Ruby on Rails: the name of the form with form_for

Whenever I work with forms in rails, I get so confused. The API documentation is not really clear regarding how to set a name for the form. There are other places that I get confused, but I will solve one problem at a time. The form_tag takes three arguments.

  1. The first argument is the url or action, and you can use the url_for syntax. This can be a string or a hash.
  2. The html options go into the second argument. This is where I would add a name option. It should be inside a hash.
  3. You can put more options in the third argument. This will be a hash.

This is what you would use for use in rails...

  <% form_for ({:action => "update", :id => @user}, {:name => "myForm" }, {}) do -%>
    ...
  <% end %>

The output should look like this...

  <form name="myForm" action="/account/update/1" method="post">
    ...
  </form>
blog comments powered by Disqus