Search code examples
ruby-on-railssimple-form-for

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


I am trying to add a basic form for product creation. I am able to crate new product and show product but when I try to edit a product I face "undefined method `model_name' for NilClass:Class "error

Here is my 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 %>

and here is the error message I am getting: _app_views_products__form_html_erb__1297814100543601928_70242432121140 app/views/products/_form.html.erb, line 1 undefined method `model_name' for NilClass:Class I tried to search Google/Stafkoverflow but I didn't find any question related to this problem. Thanks in advance for any help


Solution

  • Make your controller to look like:-

    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