Alexander Shlenchack
Alexander Shlenchack

Reputation: 3869

Parameters without name model

I have a model like this:

class Attribute < ActiveRecord::Base
  attr_accessible :name
end

and parameters in post action:

name='foo' (not attribute[name])

In Create action I can to create Attribute like this:

attribute=Attribute.new(:name => params[:name])

How tell rails parse every parameter like the model attribute?

attribute=Attribute.new(params[:attribute])

Upvotes: 0

Views: 135

Answers (1)

Kenny Grant
Kenny Grant

Reputation: 9623

Assuming you can't fix the form to submit the param in the conventional way, you can do this in your controller by editing params before creation:

params[:attribute][:name] = params[:name]
attribute=Attribute.new(params[:attribute])

or if you had a lot of params all of which you want in attribute you could just use:

attribute=Attribute.new(Hash.new(:attribute => params))

Upvotes: 1

Related Questions