无法显示ruby中url参数的视图

时间:2011-11-14 16:35:31

标签: ruby-on-rails ruby ruby-on-rails-3

我试图显示与url参数上显示的相同类别的博客列表。但是,当我点击链接时,它仍然保持在同一页面。

路线

match '/microposts/:category', :to => 'microposts#category_list'

视图

<h2>sidebar</h2>
    <% @categories= Micropost.select("category").group("category")%>
    <% unless @categories.nil? %>
    <ul><% @categories.each do |category| %>
    <li><%= link_to category.category, :controller =>"microposts", :category => category.category,     :method => 'category_list' %></li>

    <% end %>
</ul>
<% end %>

点击链接后输入category_list视图。

category_list.html.erb

<h2>Catergory</h2>
<% @microposts.each do |post|%>
<article>
    <h4><%= link_to post.title, post %>|        <%= post.created_at %></h4>
</article>
<% end %>
<%= will_paginate @microposts %>

微博控制器

def category_list
    @micropost = Micropost.select("category").where(params[:category])
    @title = "Category"     
end

1 个答案:

答案 0 :(得分:0)

好的,试试这个:

<强>更新:

microposts_controller.rb

def category_list
  @microposts = Micropost.where('category = ?', params[:category])
  @categories = Micropost.select('DISTINCT(category)').map(&:category).compact
  ...
end

的routes.rb

match '/microposts/:category' => 'microposts#category_list', :as => :microposts_category_list

_sidebar.html.erb

<h2>sidebar</h2>
<% unless @categories.empty? %>
  <ul>
    <% @categories.each do |category| %>
      <li><%= link_to category, microposts_category_list_path(category) %></li>
    <% end %>
  </ul>
<% end %>