dean
October 25, 2006, 11:20pm
1
Hi Rails,
I have a string in my controller that I want to make into a model name.
EG:
Class LlamaController < ApplicationController
def addsomething
model = params[:model_name]
# Here, I would like to take the string in model and use it as a
Model
@foo = Model.new
end
end
Is this possible? Is there a better way?
Thanks
–Dean
dean
October 25, 2006, 11:28pm
2
end
Is this possible? Is there a better way?
@foo = Class.const_get(model).new
I think.
dean
October 25, 2006, 11:34pm
3
Cool, thanks.
0 urzatron app/models % …/…/script/console
Loading development environment.
Class.const_get(‘User’).new
=> #<User:0xf7151cd0 @attributes={“created_on”=>nil, “updated_on”=>nil,
“password”=>"", “login”=>"", “css_style”=>“default”}, @new_record=true>
dean
October 26, 2006, 12:10am
4
On Wednesday 25 October 2006 23:17, Dean wrote:
@foo = Model.new
end
end
Is this possible? Is there a better way?
model.constantize
or
model.camelize.constantize
Inflector magic…
Michael
–
Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/