views:

468

answers:

4

Hey guys,

I have comments as a polymorphic model

its attached to posts, reviews, etc.

I also have an action in comments, called test for example

I have my routes setup, so test_post_comment_path works (to call the test action in the comments controller)

the problem is, in my partial view, I want that route to be able to change, based on the current action, ie. its test_post_comment_path when on a post and test_review_comment_path when on a review

any ideas?

+1  A: 

Just use two different paths?

What I mean is: you don't want to put so much logic inside routes.

If routes try to do something more than routing, the first time somethings goes wrong you'll be in serious trouble.

In your partial view, the logic to create specific links or other html comment stuff should go in a helper.

giorgian
I want the logic to change in the view itself - I'm just not sure how
Elliot
A: 

I decided to just use an if statement in the view, based on if the current action was present, such as if @post or if @review

Elliot
Look over the polymorphic path helpers.http://api.rubyonrails.org/classes/ActionController/PolymorphicRoutes.html
vise
A: 

Something like this: (in your partial view)

@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 review, it will generate test_review_comment_path

ez
+2  A: 

The correct way to do this is with polymorphic_url...

Stephen
awesome, I was just looking for that =)
Victor Martins