Getting values from @params["variable"] with a string


#1

I want to retrieve values from params but cannot hardcode in the value
in the brackets [:value]. I want to be able to have a variable in the
bracket or a string, but it seems that it doesn’t evaluate what is in
the brackets.

Any idea of how this can be done

I want to loop through a bunch of column names and store a value from a
text field of the same name

for column in Particular.content_columns
@particular.'column.name'= params[:'column.name']
end

However whatever is put between the ’ ’ seems to have to be hard coded?

Suggestions?

Much appreciated


#2

Chris wrote:

I want to loop through a bunch of column names and store a value from a
text field of the same name

for column in Particular.content_columns
  @particular.'column.name'= params[:'column.name']
    @particular.send("#{column.name}=", params[column.name.to_sym])
end

You may want to cache the “#{column.name}=” strings somewhere so it
isn’t constructed on each iteration. You could look it up in a hash that
maps column.name to the appropriate string or symbol.


#3

Beautiful! Thanks Joel