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 <form>
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.
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
<h1>New photo</h1>
<h1>Upload your picture and enter a description !</h1>
<p>
<% form_for(:photo,
@photo, :url=>{:action=>'create'}, :html=>{:multipart=>true}) do |
form> %>
Country : <%= form.text_field :country %><br/>
City : <%= form.text_field :city %><br/>
Title : <%= form.text_field :title %><br/>
Description :<%= form.text_field :description %><br/>
Upload : <%= form.file_field :image_file %><br/>
<%= 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 %>
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 } %>
=====
code for _form.rthml
<%= error_messages_for 'photo' %>
<!--[form:photo]-->
<p><label for="photo_country">Country</label><br/>
<%= text_field 'photo', 'country' %></p>
<p><label for="photo_city">City</label><br/>
<%= text_field 'photo', 'city' %></p>
<p><label for="photo_title">Title</label><br/>
<%= text_field 'photo', 'title' %></p>
<p><label for="photo_description">Description</label><br/>
<%= text_field 'photo', 'description' %></p>
<p><label for="photo_binary_data">Upload New Image</label><br/>
<%= file_field 'photo', 'binary_data' %></p>
<!--[eoform:photo]-->
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