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