我正在尝试渲染评论部分使用与博客和视频模型。这里是博客显示页面,请求评论部分,并将@blog作为模型(我将在视频的显示页面上传递@video):
<%= render :partial => 'comments/comments', :locals => {:model => @blog} %>
下面的代码将注释按最新的优先/最旧的优先排序:
<% if @comments.count > 1 %>
<span class="list_order">
<%= link_to('Newest First', model, :order => "DESC", :anchor => "comments") + " | " +
link_to('Oldest First', model, :order => "ASC", :anchor => "comments") %>
</span>
<% end -%>
当我说:
link_to('Newest First', blog_path(@blog, :order => "DESC".... etc.)
但我知道你也可以只是通过:
link_to('Newest First', @blog)
,它会自动转到博客显示页面。因此,在我的代码中,我在本地传递"模型",它确实刷新了页面,但它不接受我的参数:order或:anchor。当只使用实例变量而不使用link_to方法上的路径时,如何传递参数?
好吧,我终于有机会问了我的一个朋友,并找到了解决方案。我需要使用多态路径。所以在上面的例子中,下面的代码可以工作:
<%= link_to('Newest First', polymorphic_path(model, :order => "DESC", :anchor => "comments") + " | " + link_to('Oldest First', polymorphic_path(model, :order => "ASC", :anchor => "comments") %>
然后它知道为所使用的变量生成正确的路径。这里有一些信息:http://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes/polymorphic_path