form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) public

Starts a form tag that points the action to an url configured with url_for_options just like ActionController::Base#url_for. The method for the form defaults to POST.

Options

  • :multipart - If set to true, the enctype is set to "multipart/form-data".
  • :method - The method to use when submitting the form, usually either "get" or "post". If "put", "delete", or another verb is used, a hidden input with name _method is added to simulate the verb over post.
  • A list of parameters to feed to the URL the form will be posted to.

Examples

  form_tag('/posts')
  # => <form action="/posts" method="post">

  form_tag('/posts/1', :method => :put)
  # => <form action="/posts/1" method="put">

  form_tag('/upload', :multipart => true)
  # => <form action="/upload" method="post" enctype="multipart/form-data">

  <% form_tag '/posts' do -%>
    <div><%= submit_tag 'Save' %></div>
  <% end -%>
  # => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
Show source
Register or log in to add new notes.
October 2, 2009
5 thanks

form_tag with named route and html class

<% form_tag position_user_card_path(@user, card), :method => :put, :class => ‘position-form’ do %>

October 7, 2011
3 thanks

How to submit current url

For example to change some kind of param on select change…

<%= form_tag({}, {:method => :get}) do %>
  <%= select_tag :new_locale, options_for_select(I18n.available_locales, I18n.locale), :onchange => "this.form.submit();" %>
<% end %>
October 24, 2012
2 thanks

To add an ID to the form

Found this the hard way, but to add an ID to the form generated by form_tag, you must explicitly make hashes.

Add ID

<%= form_tag({:action => 'create'}, {:id => 'anID'}) %>
October 16, 2013
1 thank

Erb tags

The <% -%> is not being used since Rails 3.

The example above should be changed to:

<%= form_tag('/posts') do %>
  <div><%= submit_tag 'Save' %></div>
<% end %>