Need help to understand form_tag vs form_for

Hi,

Need help to understand and differences when to use form_tag and
form_for…

I have a simple app that runs on creating article and allow user to
upload picture. I have no problem to have a form using form_for to
create a form to allow user to create a new article and upload an
image.

see below…new.rhtml

New photo

Upload your picture and enter a description !

<% form_for(:photo, @photo, :url=>{:action=>'create'}, :html=>{:multipart=>true}) do | form| %> Country : <%= form.text_field :country %>
City : <%= form.text_field :city %>
Title : <%= form.text_field :title %>
Description :<%= form.text_field :description %>
Upload : <%= form.file_field :image_file %>

<%= submit_tag “Create” %>
<% end %>

<%= link_to ‘Back’, :action => ‘list’ %>

But when i need user to update the form, allowing them to upload
another new picture to overwrite the previous picture, i cant…the
code is below

see below…edit.rhtml

<% form_for(:photo, :id => @photo.id, :url =>{:action
=>‘update’}, :html=>{:multipart=>true}) do |form| %>

<%= render :partial => ‘form’ %>
<%= submit_tag ‘Edit’ %>
<% end %>

=====
code for _form.rthml

<%= error_messages_for ‘photo’ %>

Country
<%= text_field 'photo', 'country' %>

City
<%= text_field 'photo', 'city' %>

Title
<%= text_field 'photo', 'title' %>

Description
<%= text_field 'photo', 'description' %>

Upload New Image
<%= file_field 'photo', 'binary_data' %>

===========

The edit.rhtml renders a html page where i am unable to capture the
article id (photo.id) for me to update the info…

It need to have an example, whereby the edit.rhtml allow multipart
=>true and at the same time capture the ActiveRecord id for update
purposes…

I have check the Agile web development book, but there is no such
example…anyone please help me ?

thks alot

On 7/8/07, pacochin [email protected] wrote:

Hi,

Need help to understand and differences when to use form_tag and
form_for…

The difference between the two is that form_tag just outputs a
tag, and form_for gives you a means of accessing some wrapped object.

For example, if you have a brand new photo, f.text_field :name would
create a blank text field for you, with an element named such that it
will be parsed nicely and made available in params[:photo] for you.
If you’re editing an existing photo, it would create a text field with
the name filled in.

<% end %> <% form_for(:photo, :id => @photo.id, :url =>{:action =>'update'}, :html=>{:multipart=>true}) do |form| %>

<%= render :partial => ‘form’ %>
<%= submit_tag ‘Edit’ %>
<% end %>

The problem here is that your call is wrong. The second argument to
form_for is the object you want to wrap. You also passed the
incorrect options, forgetting the photo id. It should look like:

form_for(:photo, @photo, :url => { :action => ‘update’, :id =>
@photo.id }, :html => { :multipart => true })

(you should consider looking at RESTful routing and map.resources,
because then your options would be “:url => photo_url(@photo)”
instead. You also get a lot of other benefits)

When you’re using a partial, you can pass it local variables. This is
useful in this case because you can pass it the form helper. You can
then write the partial as if the helper exists, without worrying where
it came from.

<%= render :partial => ‘form’, :locals => { :form => form } %>

<%= text_field ‘photo’, ‘city’ %>

This can become

<%= form.text_field :country %>
etc

===========

The edit.rhtml renders a html page where i am unable to capture the
article id (photo.id) for me to update the info…

It need to have an example, whereby the edit.rhtml allow multipart
=>true and at the same time capture the ActiveRecord id for update
purposes…

Like I mentioned at the beginning, it looks like you just goofed the
form_for call a bit.

hth

Pat

thanks Pat !

It works! I just changed the form_for according to your advise and it
works ! Becos i am storing the image as binary file in the db…

I am slowly getting a hang of it… thanks ! just one more thing :

(you should consider looking at RESTful routing and map.resources,
because then your options would be “:url => photo_url(@photo)”
instead. You also get a lot of other benefits)

Could you advise me where to learn up more info about RESTful routing
and map.resources ? I need to understand deeper how i can take
advantage of it.

thanks for your help Pat !
really appreciate it.