0

我有一个滑块,其中包含用户可以浏览的几篇文章

现在我有

html

 <%= link_to 'read more', @article %>

控制器

 class StaticPagesController < ApplicationController
 def home
    @user = current_user
    @article = Article.find(1)
 end

 def comics
 end
 end

这链接到第一篇文章,因为它说 find(1) 但我不太确定如何在我的控制器中编写它,以便我可以使用相同的 <%= link_to 'read more', @article %> 它将链接到我想要的文章。

我希望能够将该链接添加到我的每个滑块并将其定向到正确的文章。

4

1 回答 1

0

我认为您应该坚持使用 Rails 约定,而不是StaticPagesController将ArticlesController之类的东西作为资源(请参阅Resource Routing: the Rails Default)。通过此设置,您可以按如下方式使用 url 助手:

<%= link_to "Read more", article_path(@article) %>

@article是您要为其生成 url 的当前文章这将使用 params[:id] 中的文章 ID 路由到 ArticleController 的 show 操作。

于 2012-12-12T19:19:43.963 回答