将 link_to 与实例变量和参数一起使用
我正在尝试渲染部分评论以与博客和视频模型一起使用。这是博客显示页面,要求部分评论并传递@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 -%>
当我say:
link_to('Newest First', blog_path(@blog, :order => "DESC".... etc.)
但我知道你也可以直接通过:
link_to('Newest First', @blog)
,它会自动转到博客展示页面。因此,在我的代码中,我传递了本地“模型”,它确实刷新了页面,但它不接受我的 :order 或 :anchor 参数。仅使用实例变量而不是 link_to 方法上的路径时如何传递参数?
I am trying to render a comments partial to use with both the blog and video models. Here's the blog show page asking for the comments partial and passing @blog as the model (I will pass @video on the video's show page):
<%= render :partial => 'comments/comments', :locals => {:model => @blog} %>
This next code is to order the comments as newest first/oldest first:
<% 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 -%>
This works fine when I say:
link_to('Newest First', blog_path(@blog, :order => "DESC".... etc.)
But I know that you can also just pass:
link_to('Newest First', @blog)
and it will automatically go to the blog show page. So in my code, I'm passing the "model" local, and it does refresh the page, but it does not take my argument for :order or :anchor. How do you pass arguments when using only the instance variable rather than the path on the link_to method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我终于有机会请教我的朋友并找到了解决方案。我需要使用多态路径。因此,在上面的示例中,以下代码有效:
然后它知道为所使用的变量生成正确的路径。以下是一些相关信息:
http://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes/polymorphic_path
Ok, I finally got the chance to ask a friend of mine and found the solution. I needed to use polymorphic paths. So in my example above, the following code works:
Then it knows to generate the right path for the variable used. Here's some info on that:
http://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes/polymorphic_path