ruby on rails - Route concern and polymorphic model: how to share controller and views? -


given routes:

example::application.routes.draw   concern :commentable     resources :comments   end    resources :articles, concerns: :commentable    resources :forums     resources :forum_topics, concerns: :commentable   end end 

and model:

class comment < activerecord::base   belongs_to :commentable, polymorphic: true end 

when edit or add comment, need go "commentable" object. have following issues, though:

1) redirect_to in comments_controller.rb different depending on parent object

2) references on views differ well

= simple_form_for comment |form| 

is there practical way share views , controllers comment resource?

you can find parent in before filter this:

comments_controller.rb

before_filter: find_parent  def find_parent   params.each |name, value|     if name =~ /(.+)_id$/       @parent = $1.classify.constantize.find(value)     end   end end 

now can redirect or whatever please depending on parent type.

for example in view:

= simple_form_for [@parent, comment] |form| 

or in controller

comments_controller.rb

redirect_to @parent # redirect show page of commentable. 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -