Marko I. Marko I. - 5 months ago 29
Ruby Question

NameError in Categories#show

So I have products and cart system in my rails app. Now I'm trying to create custom page, under custom controller, to showcase each product from certain category. When I include a form_for into a div class of a product, (in order to submit a form and move product to a cart), I get this error:

Showing /home/ubuntu/workspace/app/views/categories/show.html.erb where line #28 raised:

undefined local variable or method `order_item' for #<#<Class:0x007fd83dfbdea8>:0x007fd85cbad0b0>
Did you mean? order_item_url


This is the form that shows error on app/views/categories/show.html.erb:

<%= form_for order_item, remote: true do |f| %>
<h4 class="text-right">Unit Price: <span style="color: green"><%= number_to_currency product.price %></span></h4>
<div class="input-group">
<%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
<div class="input-group-btn">
<%= f.hidden_field :product_id, value: product.id %>
<%= f.submit "Add to Cart", class: "btn btn-primary" %>
</div>
</div>
<% end %>


These are my controllers:

class ProductsController < ApplicationController
def index
@products = Product.all
@order_item = current_order.order_items.new
end

def new
@product = Product.new @categories = Category.all.map{|c| [ c.name, c.id ] }
end


def create
@product = Product.new(product_params)
@product.category_id = params[:category_id]
respond_to do |format|
if @product.save

format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

def edit
@categories = Category.all.map{|c| [ c.name, c.id ] }
end

def update
@product.category_id = params[:category_id]
end


end


order_items_controller.rb:

class OrderItemsController < ApplicationController
def create
@order = current_order
@order_item = @order.order_items.new(order_item_params)
@order.user_id = current_user.id
@order.save
session[:order_id] = @order.id
end



def update
@order = current_order
@order_item = @order.order_items.find(params[:id])
@order_item.update_attributes(order_item_params)
@order_items = @order.order_items
end



def destroy
@order = current_order
@order_item = @order.order_items.find(params[:id])
@order_item.destroy
@order_items = @order.order_items
end
private
def order_item_params
params.require(:order_item).permit(:quantity, :product_id, :user_id)
end
end


and associated routes:

Rails.application.routes.draw do

resources :categories
resources :products, only: [:index]
resource :cart, only: [:show]
resources :order_items, only: [:create, :update, :destroy, :new]
root to: "products#index"

Answer

You are in a view belonging to the Categories controller.

You need to add, in your show action of the categories controller:

# CategoriesController
def show
 @order_item = OrderItem.new # Add this line
 # rest of your code
end

And replace in your form_for order_item by @order_item