Newbie - Params passed to new

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!

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.

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.

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...

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.