简体   繁体   中英

Ruby on Rails error: undefined method for nil:NilClass

I am doing my first ruby on rail project, and have the "undefined method `title' for nil:NilClass" error.

Here are the codes

============index.html.erb========

        <table>
         <% @products.select {|p| p.price.to_i > 200 }.each do |product|%>
         <tr>
            <td><%= product.title %></td>
            <td><%= product.price %></td>

         </tr>
         <% end %>
       </table>

=============products_controller.rb=========

      class ProductsController < ApplicationController
      def index
          @products = Product.all
       end

       def show
          if params[:id] == "ALL"
             @products = Product.all
          else
             @product = Product.find(params[:id])
          end

               Prefix Verb   URI Pattern                                                                              Controller#Action
        welcome_index GET    /welcome/index(.:format)                                                                 welcome#index
             products GET    /products(.:format)                                                                      products#index
                      POST   /products(.:format)                                                                      products#create
          new_product GET    /products/new(.:format)                                                                  products#new
         edit_product GET    /products/:id/edit(.:format)                                                             products#edit
              product GET    /products/:id(.:format)                                                                  products#show
                      PATCH  /products/:id(.:format)                                                                  products#update
                      PUT    /products/:id(.:format)                                                                  products#update
                      DELETE /products/:id(.:format)                                                                  products#destroy
                 root GET    /                                                                                        welcome#index

=============

All I want to do is display all product if parameter is "ALL", display some products(price < 50) if parameter is "ONSALE", and individual product when parameter is the product id. Any suggestion is welcome!

if I read your source code it seem you just need to list product that has price above 200 you can do with where , later probably if you want option to filter data more advance you can learn ransack gem

this for your index.html.erb

<table>
  <% @products.each do |product| %>
    <tr>
      <td><%= product.title %></td>
      <td><%= product.price %></td>
    </tr>
  <% end %>
</table>

this your ProductsController

class ProductsController < ApplicationController
  def index
    # if you want directly to filter just the list product that has price above 200 
    # then you can use where, your view just to show the result of @products

    @products = Product.where('price > ?',200)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM