I’m trying to take form data and put it into a model variable without
using ActiveRecord’s features (save, create, update_attributes, etc.)
that would write the record out to a database.
So I’ve written a method like this in my Customer model:
def modify(cust_info)
cust_info.each do |formfield, value|
formfield = value
end
end
And Ive got a form where some of the form fields are all set up such
that name=“customer[the_field_name]”
I’ve got a controller that has a customer object defined and I just
want to modify certain fields based on what was submitted by use of
the form:
customer.modify(params[:customer])
But the modify method I defined (see above) doesn’t appear to work.
How can I go through the hash and have each key in it be used to
identify the appropriate field in the model’s object to assign the
value?
I’m trying to take form data and put it into a model variable without
using ActiveRecord’s features (save, create, update_attributes, etc.)
that would write the record out to a database.
Whenever you want a variation on an ActiveRecord method, it’s always
useful to check the implementation of the nearest candidate. You are
trying to re-implement #update_attributes without the save. It seems
reasonable to look at the documentation first:
why exactly do you want to avoid using the standard methods? You’re
losing a lot of functionality that you’d otherwise receive for free.
how does your method not work? after you call customer.modify…, do
the corresponding attributes not have the new values set? or are you
trying to figure out why they’re not saved to the database?
Mike
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.