Polymorphic Routes in Rails - in views

前端 未结 4 852
执念已碎
执念已碎 2021-01-15 03:19

I have Comment as a polymorphic model.

It is attached to Post, Review, etc.

I also have an action in CommentsController, called test.

I

相关标签:
4条回答
  • 2021-01-15 03:32

    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

    0 讨论(0)
  • 2021-01-15 03:34

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

    0 讨论(0)
  • 2021-01-15 03:49

    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

    0 讨论(0)
  • 2021-01-15 03:54

    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.

    0 讨论(0)
提交回复
热议问题