在rails 4上创建类别链接ruby时出错

时间:2015-12-23 21:30:53

标签: ruby-on-rails-4

尝试创建指向我的类别的链接时出错: 我收到错误...我可以成功地在数据库中创建类别,并且它们与列表相关联...我唯一的问题是创建一个链接,按类别显示我在这里做错了什么?在此先感谢我一个新手,有点卡住了!

NoMethodError in ListingsController#index
undefined method `id' for nil:NilClass

更多错误:

Extracted source (around line #15):
13
14
15
16
17
18

      @listings = Listing.all.order("created_at DESC")
    else
      @category_id = Category.find_by(name: params[:catgeory]).id
      @listings = Listing.where(category_id: @category_id).order("created_at DESC")
    end
  end

我的标题代码:

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">Yahozzle</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><%= link_to "About", pages_about_path %> </li>
        <li><%= link_to "Contact", pages_contact_path %> </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Browse<b class="caret"></b></a>
          <ul class="dropdown-menu">
            <% Category.all.each do |category| %>
            <li><%= link_to category.name, listings_path(category: category.name) %></li>
            <% end %>
          </ul>
        </li>   
      </ul>

      <ul class="nav navbar-nav navbar-right">
        <% if user_signed_in? %>


              <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Seller Account <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><%= link_to "Manage Listings", seller_path %></li>
            <li><%= link_to "Sales History", sales_path %></li>
          </ul>
        </li>

        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown"><%= current_user.name %> <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><%= link_to "Purchase History", purchases_path %></li>
            <li><%= link_to "Edit Profile", edit_user_registration_path %></li>
            <li><%= link_to "Sign Out", destroy_user_session_path, method: :delete %></li>
          </ul>
        </li>
      <% else %>
        <li><%= link_to "Sign Up", new_user_registration_path %></li>
        <li><%= link_to "Sign In", new_user_session_path %></li>
      <% end %>
    </ul>
  </div><!-- /.navbar-collapse -->
</nav>

和我的控制器

def index
    if params[:category].blank?
      @listings = Listing.all.order("created_at DESC")
    else
      @category_id = Category.find_by(name: params[:catgeory]).id
      @listings = Listing.where(category_id: @category_id).order("created_at DESC")
    end
  end

1 个答案:

答案 0 :(得分:1)

第15行有一个拼写错误 - catgeory而不是category。

Category.find_by(name: params[:catgeory]).id

所以find_by返回nil,你正在调用id。只需修复拼写错误,但即便如此,您应该检查类别是否存在,如果不存在则可能显示404.