constantize error "wrong constant name post"

29 views
Skip to first unread message

fugee ohu

unread,
Jun 13, 2017, 10:15:45 PM6/13/17
to Ruby on Rails: Talk
commentable = commentable_type.constantize.find(commentable_id)

NameError in CommentsController#new
wrong constant name post

Frederico Martins

unread,
Jun 14, 2017, 9:44:52 AM6/14/17
to Ruby on Rails: Talk
Hi. Could you try the code below?

commentable = commentable_type.camelize.constantize.find(commentable_id)

fugee ohu

unread,
Jun 26, 2017, 5:18:39 PM6/26/17
to Ruby on Rails: Talk


I solved the problem by using values for polymorphic types that start with capital letters This looks like a better solution Thanks If constantize doesn't convert the first char to upper case then what does it do?

Frederico Martins

unread,
Jun 28, 2017, 9:29:26 AM6/28/17
to Ruby on Rails: Talk
It only tries to find and return an available constant with the specified name. But the string needs to be already in camel case.

Reply all
Reply to author
Forward
0 new messages