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... :) ) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Reghellin wrote:> 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... :) ) >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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") -%>"> <td><%= e.entry_date -%></td> <td><%= e.title -%></td> <td><%= e.content -%></td> <td><%= check_box( ''e[]'',''visible'',{},true,false) -%></td> <td><%= link_to ''Destroy'', { :action => ''delete_entry'', :id => e.id }, { :confirm => ''Would?'' } -%></td> <td><%= link_to ''Modify'', { :action => ''editEntry'', :id => e.id } -%></td> </tr> <% end -%><!-- fine ciclo --> The main view: <% form_for :entry, :url => { :action => :update_entry, :id => @entry } do |f| %> <div id="fields"> <%= render :partial => "form", :locals => { :f => f } %> <input name="commit" type="submit" value="Save changes" id="submit" /> </div> <% end %> the form partial (that does not show the fields filled with existing data): <div class="fieldBox"> <label for="entry_date">Entry Date</label> <%= f.date_select :entry_date, :start_year => 2007 %> </div> <div class="fieldBox"> <label for="entry_title">Title</label> <%= f.text_field :title %> </div> <div class="fieldBox"> <label for="entry_content">Content</label> <%= f.text_area :content %> </div> 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? :P -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Reghellin wrote:> 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") -%>"> > <td><%= e.entry_date -%></td> > <td><%= e.title -%></td> > <td><%= e.content -%></td> > <td><%= check_box( ''e[]'',''visible'',{},true,false) > -%></td> > <td><%= link_to ''Destroy'', > { :action => ''delete_entry'', :id => e.id }, > { :confirm => ''Would?'' } > -%></td> > <td><%= link_to ''Modify'', { :action => ''editEntry'', :id > => e.id } -%></td> > </tr> > <% end -%><!-- fine ciclo --> > > > The main view: > > <% form_for :entry, :url => { :action => :update_entry, :id => @entry } > do |f| %> > <div id="fields"> > <%= render :partial => "form", :locals => { :f => f } %> <input > name="commit" type="submit" value="Save changes" id="submit" /> > </div> > <% end %> > > the form partial (that does not show the fields filled with existing > data): > > <div class="fieldBox"> > <label for="entry_date">Entry Date</label> > <%= f.date_select :entry_date, :start_year => 2007 %> > </div> > > <div class="fieldBox"> > <label for="entry_title">Title</label> > <%= f.text_field :title %> > </div> > > <div class="fieldBox"> > <label for="entry_content">Content</label> > <%= f.text_area :content %> > </div> > > 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? :P > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nothing works. But I''m quite sure I''m doing something stupid :P 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 Pratt 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 Reghellin wrote: >> <% @e=e %> >> <td><%= link_to ''Modify'', { :action => ''editEntry'', :id >> <%= render :partial => "form", :locals => { :f => f } %> <input >> </div> >> >> else >> > -- > Sincerely, > > William Pratt-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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| %> <div id="fields"> <%= render :partial => "form", :locals => { :f => f } %> <input name="commit" type="submit" value="Save changes" id="submit" /> </div> <% 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? :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---