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!

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.

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.

On Jun 7, 1:55 pm, Jacob A. [email protected] 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.

Or even better yet use:

<% form_for :contact, contacts_path() do |form| %>

Name: <%= form.text_field :name, :size => 40 %> ... ...

instead of

<% form_tag … %>

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…

Mojave wrote:

On Jun 7, 1:55 pm, Jacob A. [email protected] 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). 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! :wink:

-Josh

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs