Forum: Ruby on Rails need help to understand form_tag vs form_for

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4ee2194b4badbb2fbdfc2a06e9376fe7?d=identicon&s=25 pacochin (Guest)
on 2007-07-09 05:22
(Received via mailing list)
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
=================
<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 %>

=====
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]-->

===========

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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-07-09 10:26
(Received via mailing list)
On 7/8/07, pacochin <pacochin@gmail.com> 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 <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.

> <p>
> <% 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'  %></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
4ee2194b4badbb2fbdfc2a06e9376fe7?d=identicon&s=25 pacochin (Guest)
on 2007-07-09 13:06
(Received via mailing list)
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.
This topic is locked and can not be replied to.