NilClass:Class 的未定义方法“model_name”:编辑简单表单时出错

undefined method `model_name' for NilClass:Class : error while editing simple form

我正在尝试添加用于产品创建的基本表单。 我能够创建新产品并展示产品,但是当我尝试编辑产品时,我遇到了 "undefined method `model_name' for NilClass:Class "error

这是我的 PrdouctControllerClass:(controllers/products_controller.rb)

class ProductsController < ApplicationController


        def index
            @products = Product.order("name")
            @products =Product.new
            @products =Product.all

      end



      def show
        @product = Product.find(params[:id])
      end

      def new
        @product = Product.new
      end

      def create
          @product = Product.new(product_params)
         if @product.save
            redirect_to @product, notice: "Successfully created product."
          else
            render :new

        end
        end

          private 

          def product_params
            params.require(:product).permit(:name, :price, :category, :ratings)
          end
        end

      def edit
        @product = Product.find (params[:id])

      end
      private
      def id_params
        params.require(:product).permit(:name, :price, :ratings, :category)
      end

      def update
        @product = Product.find(params[:product])
        redirect_to @product, notice: " Successfully updated product"
      else 
        render :edit

      end

      def destroy
        @products = Product.find (params[:id])
        @product.destroy
        redirect_to products_url, notice: "successfully destroyed products"
      end


**and here is the form (App/Views/products/_form.html.erb**)


<%= simple_form_for(@product) do |f| %>
  <% if @product.errors.any?%>
    <div class="error_messages">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="field">
    <%= f.label :released_on %><br />
    <%= f.date_select :released_on %>
  </div>
  <div class="field">
    <%= f.check_box :discontinued %>
    <%= f.label :discontinued %>
  </div>
  <div class="field">
    <%= f.label :rating %><br />
    <%= f.radio_button :rating, 1 %> 1
    <%= f.radio_button :rating, 2 %> 2
    <%= f.radio_button :rating, 3 %> 3
    <%= f.radio_button :rating, 4 %> 4
    <%= f.radio_button :rating, 5 %> 5
  </div>

  <div class="field">
    <%= hidden_field_tag "product[category_ids][]", nil %>
    <% Category.all.each do |category| %>
      <%= check_box_tag "product[category_ids][]", category.id, @product.category_ids.include?(category.id), id: dom_id(category) %>
      <%= label_tag dom_id(category), category.name %><br />
    <% end %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是我收到的错误消息: _app_views_products__form_html_erb__1297814100543601928_70242432121140 app/views/products/_form.html.erb,第 1 行 NilClass:Class 的未定义方法 `model_name' 我试图搜索 Google/Stafkoverflow 但我没有找到与此问题相关的任何问题。 在此先感谢您的帮助

让你的控制器看起来像:-

class ProductsController < ApplicationController
  def index
    @products = Product.order("name")
    @product =Product.new
    #@products =Product.all
  end

  def show
    @product = Product.find(params[:id])
  end

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(product_params)
    if @product.save
      redirect_to @product, notice: "Successfully created product."
    else
      render :new        
    end
  end  

  def edit
    @product = Product.find (params[:id])  
  end

  def update
    @product = Product.find(params[:id])
    if @product.update(product_params)
        redirect_to @product, notice: " Successfully updated product"
    else 
      render :edit
    end
  end

  def destroy
    @products = Product.find (params[:id])
    @product.destroy
    redirect_to products_url, notice: "successfully destroyed products"
  end
  private

  def product_params
    params.require(:product).permit(:name, :price, :ratings, :category_ids => [])
  end
end