My understanding is that "params" is a hash containing the parameters passed to a controller. So within a method, I should be able to do: contact = Contact.new( params ) but I get the error "undefined method `action=''" When I use: contact = Contact.new( :name => params[:name], :address => params[:address] ) everything is fine. But this of course becomes a problem when I have many parameters. I thought ActiveRecord''s new() would automatically throw out parameters that weren''t part of the data structure. I suppose I''m missing something obvious, but I can''t figure it out. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mojave wrote:> My understanding is that "params" is a hash containing the parameters > passed to a controller. So within a method, I should be able to do: > > contact = Contact.new( params ) > > but I get the error "undefined method `action=''" > > When I use: > > contact = Contact.new( :name => params[:name], :address => > params[:address] ) > > everything is fine. But this of course becomes a problem when I have > many parameters. I thought ActiveRecord''s new() would automatically > throw out parameters that weren''t part of the data structure. I > suppose I''m missing something obvious, but I can''t figure it out.No, what you need to do is "scope" the params. So that in your form you have <input type="text" name="contact[name]" /> etc. Then you may use Contact.new(params[:contact]) in your controller. The point being that everything related to a contact is put into the params[:contact] hash. If you use the built in helpers for creating your input tags, you get the "scoping" for free. -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> My understanding is that "params" is a hash containing the parameters > passed to a controller. So within a method, I should be able to do: > > contact = Contact.new( params ) > > but I get the error "undefined method `action=''" > > When I use: > > contact = Contact.new( :name => params[:name], :address => > params[:address] ) > > everything is fine. But this of course becomes a problem when I have > many parameters. I thought ActiveRecord''s new() would automatically > throw out parameters that weren''t part of the data structure. I > suppose I''m missing something obvious, but I can''t figure it out.I don''t think it does... but I''ve never really looked. Typically in your example above I see things like: contact = Contact.new( params[:contact] ) so the form variables have names such as "params[:contact][:name]" etc... That way you only deal with what you want. Look into the form helper calls for more info. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jun 7, 1:55 pm, Jacob Atzen <j...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote:> Mojave wrote: > > My understanding is that "params" is a hash containing the parameters > > passed to a controller. So within a method, I should be able to do: > > > contact = Contact.new( params ) > > > but I get the error "undefined method `action=''" > > > When I use: > > > contact = Contact.new( :name => params[:name], :address => > > params[:address] ) > > > everything is fine. But this of course becomes a problem when I have > > many parameters. I thought ActiveRecord''s new() would automatically > > throw out parameters that weren''t part of the data structure. I > > suppose I''m missing something obvious, but I can''t figure it out. > > No, what you need to do is "scope" the params. So that in your form you > have <input type="text" name="contact[name]" /> etc. Then you may use > Contact.new(params[:contact]) in your controller. The point being that > everything related to a contact is put into the params[:contact] hash. > If you use the built in helpers for creating your input tags, you get > the "scoping" for free. > > -- > Cheers, > - Jacob AtzenThanks for both responses. I''m creating a contact via an Ajax call, not using a form. The url is "/contact/add?name=joe&address=Broadway" - so in this case, I should scope it with "/contact/add? contact[name]=joe&contact[address]=Broadway" - is that right? That seems to be a little excessive, but I suppose cleaner. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> On Jun 7, 1:55 pm, Jacob Atzen <j...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote: >> Mojave wrote: >>> My understanding is that "params" is a hash containing the parameters >>> passed to a controller. So within a method, I should be able to do: >> >>> contact = Contact.new( params ) >> >>> but I get the error "undefined method `action=''" >> >>> When I use: >> >>> contact = Contact.new( :name => params[:name], :address => >>> params[:address] ) >> >>> everything is fine. But this of course becomes a problem when I have >>> many parameters. I thought ActiveRecord''s new() would automatically >>> throw out parameters that weren''t part of the data structure. I >>> suppose I''m missing something obvious, but I can''t figure it out. >> >> No, what you need to do is "scope" the params. So that in your form you >> have <input type="text" name="contact[name]" /> etc. Then you may use >> Contact.new(params[:contact]) in your controller. The point being that >> everything related to a contact is put into the params[:contact] hash. >> If you use the built in helpers for creating your input tags, you get >> the "scoping" for free. >> >> -- >> Cheers, >> - Jacob Atzen > > Thanks for both responses. > > I''m creating a contact via an Ajax call, not using a form. The url is > "/contact/add?name=joe&address=Broadway" - so in this case, I should > scope it with "/contact/add? > contact[name]=joe&contact[address]=Broadway" - is that right? > > That seems to be a little excessive, but I suppose cleaner.Or just pull out what you want and name it explicitly in your controller like you had been doing... Or do something with Contact.column_names to dynamically pull what you want out of params or something... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Or even better yet use: <% form_for :contact, contacts_path() do |form| %> <p> <label for="name">Name:</label> <%= form.text_field :name, :size => 40 %> ... ... instead of <% form_tag .... %> ... ... On Jun 7, 4:55 pm, Jacob Atzen <j...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote:> Mojave wrote: > > My understanding is that "params" is a hash containing the parameters > > passed to a controller. So within a method, I should be able to do: > > > contact = Contact.new( params ) > > > but I get the error "undefined method `action=''" > > > When I use: > > > contact = Contact.new( :name => params[:name], :address => > > params[:address] ) > > > everything is fine. But this of course becomes a problem when I have > > many parameters. I thought ActiveRecord''s new() would automatically > > throw out parameters that weren''t part of the data structure. I > > suppose I''m missing something obvious, but I can''t figure it out. > > No, what you need to do is "scope" the params. So that in your form you > have <input type="text" name="contact[name]" /> etc. Then you may use > Contact.new(params[:contact]) in your controller. The point being that > everything related to a contact is put into the params[:contact] hash. > If you use the built in helpers for creating your input tags, you get > the "scoping" for free. > > -- > Cheers, > - Jacob Atzen--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mojave wrote:> On Jun 7, 1:55 pm, Jacob Atzen <j...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote: >> > contact = Contact.new( :name => params[:name], :address => >> everything related to a contact is put into the params[:contact] hash. >> If you use the built in helpers for creating your input tags, you get >> the "scoping" for free. >> >> -- >> Cheers, >> - Jacob Atzen > > Thanks for both responses. > > I''m creating a contact via an Ajax call, not using a form. The url is > "/contact/add?name=joe&address=Broadway" - so in this case, I should > scope it with "/contact/add? > contact[name]=joe&contact[address]=Broadway" - is that right? > > That seems to be a little excessive, but I suppose cleaner.This might be a good time to look into routing (http://api.rubyonrails.org/classes/ActionController/Routing.html). You should be able to create a creative route to handle the details (I haven''t tested this, so no promises): map.connect ''/contact/add?name=:contact[name]&address=:contact[address]'', :controller => contact, :action => add At least this way you keep the url ugliness from your customer. Otherwise, you can always setup something like: map.connect ''/contact/add?*details'', :controller => contact, :action => add and then your contact details will be passed in their own variable. The problem I''ve found with params is that it ends up containing a lot of details about the rails transaction that you, most likely, don''t need. Also, there''s nothing wrong with parsing out the params and then creating the row. Don''t worry, Ruby won''t bite! ;-) -Josh -- 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 -~----------~----~----~----~------~----~------~--~---
rein.henrichs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-08 11:13 UTC
Re: Newbie - Params passed to new
I''m not exactly sure of the value of routing here. The form will be POSTed and the user will never see them in the url. Also, while you should indeed scope your form params by nesting them, you shouldn''t be writing the html code manually. Use rails'' built-in form helpers. They are abundant.Start by reading the documentation for form_for. Then you can indeed simply use contact = Contact.new(params[:contact]). Also, ActiveRecord will indeed bork if fed params that don''t match up with the model''s attributes. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---