difference between form_for and form_tag

Answer Posted / arpita patel

form_for and form_tag both are used to submit the form in
ruby on rails.
but the way of handling objects related to model is
different.

form_for:

you should use form_for for a specific model i.e while
crating an new row in database. form_for will perform the
standard http post which is having fields related to active
record objects.

here is the example for using form_for in ruby on rails:

<% form_for :user, @user, :url => { :action => "update" }
do |f| %>

then in here you can use the f object to create input
field.

First name: <%= f.text_field :firstname %>
Last name : <%= f.text_field :lastname %>
Biography : <%= f.text_area :biography %>

<% end %>

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:

<% form_tag '/posts' do -%>
<%= text_field_tag "post", "firstname" %>
<% end -%>

Is This Answer Correct ?    54 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is the command to create a migration?

703


Explain the features of ruby on rails.

594


What is the process for creating a controller for the subject? What is its role?

572


what is the function of garbage collection in Ruby on Rails?

653


What is the use of super in Ruby Rails?

673


What are the different components of rails ?

660


Tell me what is rails migration?

605


what is the log that has to seen to check for an error in ruby rails?

611


How many types of relationships does a model has?

621


How many filters are there in rails.

666


how can you declare a block in Ruby?

676


What is the rail console?

646


What I have to do to view my project always on browser?

637


what is different between activerecord::relation's count, length and size methods?

673


In how many ways you can create rails layout http response.

586