Rails 中的多态路由 - 在视图中 [英] Polymorphic Routes in Rails - in views
问题描述
我将评论作为多态模型.
I have Comment as a polymorphic model.
它附加到 Post、Review 等.
It is attached to Post, Review, etc.
我在 CommentsController 中还有一个动作,叫做 test
.
I also have an action in CommentsController, called test
.
我已经设置了路由,所以 test_post_comment_path
可以工作(在 CommentsController 中调用 test
操作).
I have my routes setup, so test_post_comment_path
works (to call the test
action in the CommentsController).
问题是,在我的部分观点中,我希望该路线能够根据当前操作进行更改,即.发布时为 test_post_comment_path
,评论时为 test_review_comment_path
.
The problem is, in my partial view, I want that route to be able to change, based on the current action, ie. it is test_post_comment_path
when on a Post and test_review_comment_path
when on a Review.
推荐答案
我决定只在视图中使用 if 语句,基于当前操作是否存在,例如 if @post 或 if @review
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
这篇关于Rails 中的多态路由 - 在视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!