form_for vs form_tag in Rails
Both form_for and form_tag are used to submit the form in
Ruby on Rails. But form_tag just outputs a <form>
tag while form_for gives a means of accessing some wrapped object.
form_for :
we used form_for for a specific model i.e while we need to
create a new row in database. so, form_for perform the standard http post method and posses active record objects.
Below is the example for using form_for form helper:
view sourceprint?
<% form_for @client do |f| %>
First name: <%= f.text_field :firstname %>
Last name : <%= f.text_field :lastname %>
Email : <%= f.text_field :email %>
Address : <%= f.text_area :address %>
<% end %>
here we are using f object of model class “Client” to create form input fields
form_tag :
form_tag just creates an form as an normal form. form_for
will perform the standard http post without any model
backed and has normal fields. this is used mainly when
specific things need to be submitted via form
here is the example for using form_tag in ruby on rails:
view sourceprint?
<% form_tag ‘/posts’ do -%>
<%= text_field_tag “post”, “firstname” %>
<% end -%>
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically each day to your feed reader.


No comments yet.
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>