# Form\_for question

**URL:** <https://www.ruby-forum.com/t/form-for-question/119062>\
**Category:** Rails\
**Created:** [November 2, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/form-for-question/119062 "2007-11-02T18:46:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/form-for-question/119062/1 "2007-11-02T18:46:35Z")

</div>

Hi! A little question, I think quite stupid, so sorry in advance 😉  
I find some differences between the official rails documentation and the  
Thomas book ‘agile web dev…’, and I don’t understand it.

I the official documentation I find examples like this:

\<% form\_for :person, @person, :html =\> {:id =\> ‘person\_form’} do |f| %\>  
…  
\<% end %\>

where @person is passed as argument.

In the book, all the examples don’t have any instance variable passed as  
an argument. The above example would be written like this:

\<% form\_for :person, :html =\> {:id =\> ‘person\_form’} do |f| %\>  
…  
\<% end %\>

Why? Anyone knows it? (I think yes, I know it must be a stupid question,  
but be patient, I’m a newbie… 🙂 )

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/form-for-question/119062/2 "2007-11-02T20:37:01Z")

</div>

Either way will work. The symbol specifies what the form fields names  
will be prefixed with and the second is the instance variable used to  
setup the form (default values, etc). If the name of the instance  
variable is the same as the symbol name then the instance variable is  
not a required argument.

-Bill

Luca R. wrote:

> where @person is passed as argument.

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/form-for-question/119062/3 "2007-11-02T20:52:03Z")

</div>

To be true, this question ha another origin: I’m setting up an update  
page. This page renders a partial of the form, used both for the create  
and update pages. I thought that passing an id to the form, the view  
would show me the field already filled with the existing data to be  
edited. But this does not happens. And I thought the reason was in the  
object I’m (maybe not) passing…

The link to the main view:

\<% @entries.each do |e| -%\>  
\<% @e=e %\>  
\<tr class="\<%= cycle(“even”, “odd”) -%\>"\>

\<%= e.entry\_date -%\>  
\<%= e.title -%\>  
\<%= e.content -%\>  
\<%= check\_box( ‘e[]’,‘visible’,{},true,false)  
-%\>  
\<%= link\_to ‘Destroy’,  
{ :action =\> ‘delete\_entry’, :id =\> e.id },  
{ :confirm =\> ‘Would?’ }  
-%\>  
\<%= link\_to ‘Modify’, { :action =\> ‘editEntry’, :id  
=\> e.id } -%\>  
  
\<% end -%\>

The main view:

\<% form\_for :entry, :url =\> { :action =\> :update\_entry, :id =\> @entry }  
do |f| %\>

 \<%= render :partial =\> "form", :locals =\> { :f =\> f } %\> 
\<% end %\>

the form partial (that does not show the fields filled with existing  
data):

 Entry Date \<%= f.date\_select :entry\_date, :start\_year =\> 2007 %\>

 Title \<%= f.text\_field :title %\>

 Content \<%= f.text\_area :content %\>

The controller (incomplete):

def edit\_entry  
@entry = Entry.find(params[:id])  
end

def update\_entry  
@entry = Entry.find(params[:id])  
if @entry.update\_attributes(params[:entry])  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘editEntry’  
end  
end

Do you maybe know why? 😛

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/form-for-question/119062/4 "2007-11-02T20:58:39Z")

</div>

Try changing the locals to this:

:locals =\> { :f =\> f, :entry =\> @entry }

Make any difference? If not, try adding @entry as the second argument to  
the form\_for.

-Bill  
Luca R. wrote:

> ```
> <% @e=e %>
> <td><%= link_to 'Modify', { :action => 'editEntry', :id 
> <%= render :partial => "form", :locals => { :f => f } %> <input 
> 
> ```
> 
> else

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 2, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/form-for-question/119062/5 "2007-11-02T21:18:12Z")

</div>

Nothing works. But I’m quite sure I’m doing something stupid 😛  
I’ll try to take a deeper look to every single piece of app, then  
eventually come back crying for some help…

Thank you anyway 🙂

William P. wrote:

> Try changing the locals to this:
> 
> :locals =\> { :f =\> f, :entry =\> @entry }
> 
> Make any difference? If not, try adding @entry as the second argument to  
> the form\_for.
> 
> -Bill  
> Luca R. wrote:
> 
> > ```
> > <% @e=e %>
> > <td><%= link_to 'Modify', { :action => 'editEntry', :id 
> > <%= render :partial => "form", :locals => { :f => f } %> <input 
> > 
> > ```
> > 
> > else
> 
> –  
> Sincerely,
> 
> William P.

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [November 4, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/form-for-question/119062/6 "2007-11-04T21:52:38Z")

</div>

Ok, I was simply doing something stupid… I wrote the edit\_entry method  
2 times, the second without any code inside (I wrote it before, as a  
place holder, and then forgot it…) so there was no @entry instance  
variable in the view…  
Now is all working fine.

BUT NOW… I’ve got another more interesting question:

\<% form\_for :entry, :url =\> { :action =\> :update\_entry, :id =\> @entry }  
do |f| %\>

 \<%= render :partial =\> "form", :locals =\> { :f =\> f } %\> 
\<% end %\>

I see a strange thing (for me, a newbe) for the :url parameter:

:id =\> @entry  
:id =\> @entry.id

They seem to do the same thing. Both set the correct id in the  
update\_entry view. But why? The first one does not set any id… But in  
view, I see action="/admin/update\_entry/5" where 5 is a correct id, and  
all the form fields filled right.

And second (even more strange…):

:url =\> { :action =\> :update\_entry }

No :id (action="/admin/update\_entry/"). But the view still show the form  
fields filled with the correct data!!! Why?

🙂
