Form_for question

Hi! A little question, I think quite stupid, so sorry in advance :wink:
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… :slight_smile: )

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.

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? :stuck_out_tongue:

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.

Nothing works. But I’m quite sure I’m doing something stupid :stuck_out_tongue:
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 :slight_smile:

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.

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?

:slight_smile: