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

write a program to show the usage of sql in the database file?

648


what is the purpose of RJs in Rails?

726


what are the positive aspects of Rails?

623


What is the purpose of load, auto_load, and require_relative in ruby ?

617


What is the difference between symbol and string?

701


What can Rails Migration do?

655


Explain rvm in rails?

598


What is the difference between false and nil in ruby?

629


What is bundler?

668


what is testing in rails?

728


How is dynamic scaffolding different from static scaffolding?

636


How many types of relationships does a model has?

632


Explain me what is the purpose of rjs in rails?

654


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

584


Explain the advantages of ruby on rails?

645