Disclaimer: Ruby/RoR nuby How do I automatically map my "params" object onto a model object from within my controller? Thanks, Jason --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jason Norris
2006-Dec-11 22:03 UTC
Re: How do you automatically map "params" onto an object?
Pretty nubish here too. If you look at scaffold methods, it will give you a hint. ActiveRecord objects can be mapped to a hash, and your params is just a hash, so if your params has within it a ''user'' key containing a hash with ''username'', ''password'', ''phone'', etc, you can do User.new(params[:user]), which is the same as doing User.new(:username => ''billj'', :password => ''billrules'', :phone => ''123-456-7890'') Thanks, Jason Jason Vogel wrote:> Disclaimer: Ruby/RoR nuby > > How do I automatically map my "params" object onto a model object from > within my controller? > > Thanks, > Jason > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jason Vogel
2006-Dec-11 22:56 UTC
Re: How do you automatically map "params" onto an object?
I was hoping to use a generic approach and not have to specific each field [column]. Does any body know of a generic way to do this and handle "extra" parms? Thanks, Jason On Dec 11, 4:03 pm, Jason Norris <jasonmnor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Pretty nubish here too. > If you look at scaffold methods, it will give you a hint. ActiveRecord > objects can be mapped to a hash, and your params is just a hash, so if > your params has within it a ''user'' key containing a hash with > ''username'', ''password'', ''phone'', etc, you can do > User.new(params[:user]), which is the same as doing > User.new(:username => ''billj'', :password => ''billrules'', :phone => > ''123-456-7890'') > Thanks, > Jason > > Jason Vogel wrote: > > Disclaimer: Ruby/RoR nuby > > > How do I automatically map my "params" object onto a model object from > > within my controller? > > > Thanks, > > Jason--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Deirdre Saoirse Moen
2006-Dec-11 23:04 UTC
Re: How do you automatically map "params" onto an object?
On 12/11/06, Jason Vogel <jasonvogel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Disclaimer: Ruby/RoR nuby > > How do I automatically map my "params" object onto a model object from > within my controller?The answer lies within the scaffold code, have you tried generating a scaffold for a model and looked at it? -- _Deirdre http://deirdre.net/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The main trick here is to match the params keys to the field names in your database. For example: --DB create table comments title varchar(50) text varchar(255) end --view form_tag :controller => ''comments'', :action => ''add'' text_field(''comment'', ''title'') text_area(''comment'', ''text'') submit_tag "Add Comment" end --Comments Controller def add Comment.create(params[:comment]) end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I think I understand: If the params hash has keys that do not match the database fields you will end up with "method not found" type errors. I have been looking for a neat way around this issue. I suspect the answer will come from the "method_missing" hook, but this may cause genuine errors to fail silently... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2006-Dec-11 23:46 UTC
Re: How do you automatically map "params" onto an object?
>User.new(params[:user]), which is the same as doing >User.new(:username => ''billj'', :password => ''billrules'', :phone => >''123-456-7890'')Jason just needed one more sentence to explain this. The point is that params[:user] is really:> PARAMS > --USER > --username => ''billj'' > password => ''billrules'' > phone => ''123-456-7890''So params[:user] returns another hash. To call them individually you have to say:> params[:user][:username] > params[:user][:password] > params[:user][:phone]So by constructing the proper fields text_field(''user'', ''email'') becomes:> params[:user][:email]and so forth. By having a model object ready to accept these hash=>key pairs you can suck them all in at once with: @user = User.new(params[:user]) In this case your model would need to be: User username password phone email -- 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 -~----------~----~----~----~------~----~------~--~---
Doh! of course! It never occurred to me to put all the model specific values into their own array on the form, which solves the "method_missing" errors. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Deirdre Saoirse Moen
2006-Dec-12 00:32 UTC
Re: How do you automatically map "params" onto an object?
On 12/11/06, askegg <andrew.skegg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I think I understand: > > If the params hash has keys that do not match the database fields you > will end up with "method not found" type errors. > > I have been looking for a neat way around this issue.In your model: attr_accessor :foo -- _Deirdre http://deirdre.net/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---