I am new to ruby on rails. I checked online and on many tutorials but I can''t find an answer to my problem :( As an exercise I am creating a weblog I am using a simple form to add a new "page" to my weblog Form code: <%= error_messages_for ''page'' %> <!--[form:page]--> <p><label for="page_title">Title</label><br/> <%= text_field ''page'', ''title'' %></p> <p><label for="page_created_at">Created at</label><br/> <%= datetime_select ''page'', ''created_at'' %></p> <p><label for="page_content">Content</label><br/> <%= text_area ''page'', ''content'' %></p> <!--[eoform:page]--> and in the controller I use @page = Page.new(params[:page]) It works but I need to modify the parameters I am sending to Page.new() I know I can''t modify :page as it is a Symbol. But how can I access the values contained inside :page? I have tried b = eval(:a.id2name) to get the variable''s contents but id does not work. Any idea? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Aurélien,> As an exercise I am creating a weblog > > I am using a simple form to add a new "page" to my weblog > > Form code: > <%= error_messages_for 'page' %> > > <!--[form:page]--> > <p><label for="page_title">Title</label><br/> > <%= text_field 'page', 'title' %></p> > > <p><label for="page_created_at">Created at</label><br/> > <%= datetime_select 'page', 'created_at' %></p> > > <p><label for="page_content">Content</label><br/> > <%= text_area 'page', 'content' %></p> > <!--[eoform:page]--> > > and in the controller I use > > @page = Page.new(params[:page]) > > It works but I need to modify the parameters I am sending to Page.new() > > I know I can't modify :page as it is a Symbol. > But how can I access the values contained inside :page?title = params[:page][:title] content = params[:page][:content] and so on... -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aurélien Bottazini wrote:> I am new to ruby on rails. I checked online and on many tutorials but > I can''t find an answer to my problem :( > > As an exercise I am creating a weblog > > I am using a simple form to add a new "page" to my weblog > > Form code: > <%= error_messages_for ''page'' %> > > <!--[form:page]--> > <p><label for="page_title">Title</label><br/> > <%= text_field ''page'', ''title'' %></p> > > <p><label for="page_created_at">Created at</label><br/> > <%= datetime_select ''page'', ''created_at'' %></p> > > <p><label for="page_content">Content</label><br/> > <%= text_area ''page'', ''content'' %></p> > <!--[eoform:page]--> > > and in the controller I use > > @page = Page.new(params[:page]) > > It works but I need to modify the parameters I am sending to Page.new() > > I know I can''t modify :page as it is a Symbol. > But how can I access the values contained inside :page? > > I have tried b = eval(:a.id2name) to get the variable''s contents but > id does not work. > > Any idea?You can use either: params[:page][:content] if you wanted to edit the values before putting it into the Page object or @page.content If you want to work on the object after initialising it. -- 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 -~----------~----~----~----~------~----~------~--~---
Aurélien Bottazini wrote:> I am new to ruby on rails. I checked online and on many tutorials but > I can''t find an answer to my problem :( > > As an exercise I am creating a weblog > > I am using a simple form to add a new "page" to my weblog > > Form code: > <%= error_messages_for ''page'' %> > > <!--[form:page]--> > <p><label for="page_title">Title</label><br/> > <%= text_field ''page'', ''title'' %></p> > > <p><label for="page_created_at">Created at</label><br/> > <%= datetime_select ''page'', ''created_at'' %></p> > > <p><label for="page_content">Content</label><br/> > <%= text_area ''page'', ''content'' %></p> > <!--[eoform:page]--> > > and in the controller I use > > @page = Page.new(params[:page]) > > It works but I need to modify the parameters I am sending to Page.new() > > I know I can''t modify :page as it is a Symbol. > But how can I access the values contained inside :page? > > I have tried b = eval(:a.id2name) to get the variable''s contents but > id does not work. > > Any idea? > > > > > >Hey The params hash is a hash of hashes... ie irb(main):010:0> params = {:page => {:foo => "bar"}} => {:page=>{:foo=>"bar"}} irb(main):011:0> params[:page][:foo] => "bar" irb(main):012:0> params[:page][:foo] = "diff" => "diff" irb(main):013:0> params[:page][:foo] => "diff" So in your example you could say: ... params[:page][:title] = "Auto Title" @page = Page.new(params[:page]) ... Goodluck, Gustav Paul --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Woops! params isn''t necessarily a hash of hashes, the insertwidget_tag helpers for instance aren''t mapped that way... <%=text_field_tag :name%> will get passed to the controller as: params[:name] Ciao Gustav Paul --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Another thing I just spotted is your use of created_at here...> <p><label for="page_created_at">Created at</label><br/> > <%= datetime_select ''page'', ''created_at'' %></p>created_at is automatically set by rails when the @page object is created. updated_at, its companion, keeps track of when the object was last saved after modification. Gustav Paul --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you to both of you. It works perfectly now.> > Another thing I just spotted is your use of created_at here... > >> <p><label for="page_created_at">Created at</label><br/> >> <%= datetime_select ''page'', ''created_at'' %></p> > created_at is automatically set by rails when the @page object is > created. updated_at, its companion, keeps track of when the object was > last saved after modification. > > Gustav PaulI used scaffold to generate the code initially. But your right created_at is automatically set by rails. I am going to remove it from the form ;) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---