Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Inflection a string to a model name in Rails?

There is one http://localhost:3000/me/posts/new?type=note

I want to create a model by the params typein my controller and I have a model named Post::Note.

so how to create it by params[:type] string ?

like image 342
why Avatar asked Jan 28 '13 12:01

why


1 Answers

Try this:

note_klass = params[:type].camelize.constantize
note = note_klass.new

RE: question edit

If your Note class is not global, you can use this:

const_name = params[:type].camelize
note_klass = Post.const_get(const_name)
like image 95
Sergio Tulentsev Avatar answered Sep 29 '22 00:09

Sergio Tulentsev