Polymorphic Routes in Rails - in Views
I have a comment as a polymorphic model.
It is attached to a post, review, etc.
I also have an action in the CommentsController called test
.
I have my routes setup, so it test_post_comment_path
works (to call the action test
in the CommentController).
The problem is that in my partial view, I want this route to be able to change depending on the current activity, i.e. it test_post_comment_path
is when on column and test_review_comment_path
when on review.
I decided to just use an if statement in the view based on whether the current action was present, for example if @post or if @review
The correct way to do it is with polymorphic_url ...
Just use two different paths?
I mean: you don't want to inject so much logic inside the routes.
If the routes are trying to do more than routing the first time something goes wrong, you are in serious trouble.
In your partial view, the logic for creating specific links or other html comment stuff should go in the helper.
Something like this: (in partial form)
@commentable.each |commentable|
test_#{commentable.class.to_s.downcase}_comment_path
end
if it is "post" then it will generate "test_post_comment_path" if it is viewed it will generate test_review_comment_path