# Newbie - Params passed to new

**URL:** <https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265>\
**Category:** Rails\
**Created:** [June 7, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265 "2007-06-07T22:48:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 7, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/1 "2007-06-07T22:48:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 7, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/2 "2007-06-07T22:50:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 7, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/3 "2007-06-07T22:51:39Z")

</div>

Mojave wrote:

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

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 8, 2007, 12:16am UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/4 "2007-06-08T00:16:48Z")

</div>

On Jun 7, 1:55 pm, Jacob A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 A.

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.

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 8, 2007, 1:47am UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/5 "2007-06-08T01:47:56Z")

</div>

Or even better yet use:

\<% form\_for :contact, contacts\_path() do |form| %\>

Name: \<%= form.text\_field :name, :size =\> 40 %\> ... ...

instead of

\<% form\_tag … %\>  
…  
…

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 8, 2007, 12:28am UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/6 "2007-06-08T00:28:49Z")

</div>

> > Contact.new(params[:contact]) in your controller. The point being that  
> > 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…

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 8, 2007, 5:42am UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/7 "2007-06-08T05:42:32Z")

</div>

Mojave wrote:

> On Jun 7, 1:55 pm, Jacob A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 A.
> 
> 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](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

---

<div class="post-metadata">

**Author:** ![Mojave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@Mojave](https://www.ruby-forum.com/u/Mojave)\
**Post date:** [June 8, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/newbie-params-passed-to-new/106265/8 "2007-06-08T13:14:11Z")

</div>

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.
