Jump to content

The ultimate community for Ruby on Rails developers.


Is there any good reason to use form_tag instead of a non-model form_for ?

form_tag form_for formtastic

  • Please log in to reply
3 replies to this topic

#1 starfry



  • Members
  • 12 posts

Posted 01 December 2013 - 08:19 PM

I've always used form_tag alongside form_for, each having their place in the problem-solving domain. However, when recently evaluating formtastic, which doesn't work with form_tag I realised that I could use form_for instead of form_tag by using form_for with a symbol (or string) instead of a model instance variable.


For example, instead of

<%= form_tag sessions_path do %>
  <%= label_tag :email %>
  <%= text_field_tag :email, params[:email] %>
  <%= label_tag :password %>
  <%= password_field_tag :password %>
  <%= submit_tag "Log in" %></div>
<% end %>

you can do

<%= form_for :session, url: sessions_path do |f| %>
  <%= f.label :email %>
  <%= f.text_field :email, value: params[:email] %>
  <%= f.label :password %>
  <%= f.password_field :password %>
  <%= f.submit "Log in" %>                                                                  
<% end %>

The only difference I can see is that the params hash has the form fields inside a hash named by the first argument to form_for (in the example case, :session). You could argue that is cleaner because the form fields are namespaced.


On the surface using form_for instead of form_tag seems like a good idea because there's only one syntax to worry about and it makes all forms consistent. However, I guess there could be reasons not immediately apparent to me why using form_for in this way may not be the same as using form_tag. I don't know what those reasons are so thought I'd ask here - perhaps someone more experienced has a view on this?


If there are scenarios where you just have to use form_tag then that would mean those scenarios could not be dealt with using formtastic.


#2 Ohm



  • Moderators
  • 530 posts
  • LocationCopenhagen

Posted 01 December 2013 - 08:52 PM



If you use the form_for syntax you can pass in an object instead of a symbol. Having e.g.

def new
  @person = Person.new

in your controller, you could do

<%= form_for @person do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

in your view. You could also have 

def edit
  @person = Person.find(params[:id])

in the same controller and use the very same view, which is why the general pattern is to do

# new.html.erb
<h1>New person</h1>
<%= render partial: "form" %>

# edit.html.erb
<h1>Edit person</h1>
<%= render partial: "form" %>

# _form.html.erb
<%= form_for @person do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

  • james likes this

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk

#3 starfry



  • Members
  • 12 posts

Posted 01 December 2013 - 11:21 PM

Don't your exapmles relate to model-based forms? Or are you're agreeing the form_for approach to forms for non-model objects is the way to go...? Seems a no-brainer to me :)

#4 james



  • Members
  • 223 posts
  • LocationLeeds, U.K.

Posted 10 December 2013 - 11:38 AM   Best Answer

Ohm has it spot on in his reply but just to clarify.


form_for will evaluate the state of the object passed in, to determine the url to post the form back to (create or update action depending on object state), form_tag does not have this overhead and is therefore slightly more performant.. Use either, whatever suits your needs. I use form_tag for non model forms as it becomes clearer to me when looking back at the code what the intended use is and it is less code to write up.

Programming is just about problem solving!

Also tagged with one or more of these keywords: form_tag, form_for, formtastic

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users