NoMethodError in Products#add , undefined method `errors' for nil:NilClass

163 Views Asked by At

i have no idea why this error come out, can anyone help me? thanks It shown when i comment out the product controller( def add) there. And also if possible is that can help me check the add.html.erb there is that my option_for_select to get all the outlets correct?

Errors show in website

Errors show in console

ActionView::Template::Error (undefined method `errors' for nil:NilClass


@virtual_path = "shared/_error_messages";object = local_assign
s[:object]; object = object;; if object.errors.any?

                                   ^^^^^^^):
1: <% if object.errors.any? %>
2:   <div id="error_explanation">
3:     <div class="alert alert-danger">
4:       The form contains <%= pluralize(object.errors.count, "error")
%>.

app/views/shared/_error_messages.html.erb:1
app/views/products/add.html.erb:5
app/views/products/add.html.erb:4

Add.html.erb

<h1>Add to outlet</h1>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_with(model: @product, local: true) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :quantity %>
  <%= f.number_field :quantity, class: 'form-control' %>

  <%= f.label :price %>
  <%= f.number_field :price, class: 'form-control' %>

  <%= f.label :outlet %>
  <%= f.select :outlet, options_for_select(@outlets), :include_blank => true %>

  <%= f.hidden_field :category_id, value: 1 %>

  <%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>

_error_messages.html.erb

<% if object.errors.any? %>
 <div id="error_explanation">
  <div class="alert alert-danger">
   The form contains <%= pluralize(object.errors.count, "error") %>.
  </div>
    <ul>
      <% object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
   </ul>
 </div>
<% end %>

Product Controller

class ProductsController < ApplicationController


def category
@category = Category.find(params[:id])
end

def index
@products = Product.all
end

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

def new
@product = Product.new
@product.category_id = params[:category_id]
end

def create
@product = Product.new(product_params)
@category_id = Category.find(params[:product]   [:category_id])
if @product.save
  flash[:success] = "Succesful create!"
  redirect_to @product
else
  render 'new'
end
end

def outlet
@outlet = Outlet.find(params[:id])
end

def add
#@product = Product.find(params[:id])
#@outlet = Outlet.find(params[:outlet_id])
end

def update
@product = Product.find(params[:id])
@outlet = Outlet.find(params[:outlet][:name])
if @product.update(product_params)
  flash[:success] = "Product updated"
  redirect_to @product
else
  render 'add'
end
end


private

def product_params
  params.require(:product).permit(:name, :quantity, :price,
                                  :category_id)
end
end
0

There are 0 best solutions below