将 link_to 与实例变量和参数一起使用

发布于 2024-11-30 11:34:29 字数 856 浏览 0 评论 0原文

我正在尝试渲染部分评论以与博客和视频模型一起使用。这是博客显示页面,要求部分评论并传递@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2024-12-07 11:34:29

好吧,我终于有机会请教我的朋友并找到了解决方案。我需要使用多态路径。因此,在上面的示例中,以下代码有效:

<%= 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

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:

<%= link_to('Newest First', polymorphic_path(model, :order => "DESC", :anchor => "comments") + " | " + link_to('Oldest First', polymorphic_path(model, :order => "ASC", :anchor => "comments") %> 

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文