1

我有一个包含标题、正文、作者和类别字段的文章模型。所有文章都按其类别分组,这意味着我有 6 个页面,例如“新闻”、“公告”、“体育”等。

我如何从其显示页面链接到文章类别页面?

文章控制器.rb

def news
      @articles = Article.paginate(page: params[:page], per_page: 7).where(category: "News")
    end

耙路线和路线.rb

    news        /news(.:format)                                   articles#new

match '/news', controller: "articles", action: "news"

关联

<b><%= link_to @article.category, @article.category_path?? %></b>

提前致谢!

4

1 回答 1

2

With rails 2

#routes.rb
map.news       'news',     :controller => "Articles", :action => "news"
map.bulletin   'bulletin', :controller => "Articles", :action => "bulletin"

This will give your news_path or bulletin_path. In a particular function, you can load the article_category as well as all its articles.

Make a helper method category_path like below

def category_path(article)
    return eval("#{article.category.downcase}_path")
end

and links like

<%= link_to @article.category, category_path(@article) %>
于 2012-10-12T12:15:21.450 回答