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 are the functions performed by rails migration?

476


Explain the advantages of ruby on rails?

490


Tell me what is the function of garbage collection in ruby on rails?

487


What is the difference between false and nil in ruby?

456


hi am b,tech 2011 passedout now am learnig ROR technology may i knw what is the feature of this one and what is the salary ranges to this i heard there is a less salaries to this technology give me valuable suggestions

1899






Write the command to run the migration.

496


What is the flash?

487


What is the main difference between procs and blocks?

467


how can we define Ruby regular expressions?

488


How is dynamic scaffolding different from static scaffolding?

455


what is Cross-Site Request Forgery (CSRF) and how Rails is protected against it?

496


what is the difference between the Observers and Callbacks in Ruby on Rails?

509


What are the servers supported by ruby on rails?

508


what is a rails migration? Write up a short example of a simple rails migration with a table called customers, a string column called name, and a text column called description?

487


How many types of associations relationships does a model have?

447