How to insert string in model name in Rails?

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

I want to create a model with type parameters in my controller, and I have a model called Post :: Note.

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

+6
source share
1 answer

Try the following:

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

RE: question editing

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

 const_name = params[:type].camelize note_klass = Post.const_get(const_name) 
+9
source

All Articles