首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError in Categories#show

NameError in Categories#show
EN

Stack Overflow用户
提问于 2016-06-12 18:13:00
回答 1查看 339关注 0票数 0

所以我的rails应用程序中有产品和购物车系统。现在,我正在尝试创建自定义页面,在自定义控制器下,展示来自特定类别的每个产品。当我将form_for包含到产品的div类中时,(为了提交表单并将产品移动到购物车),我得到以下错误:

代码语言:javascript
复制
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

这是显示app/视图/类别/show.html.erb上的错误的表单:

代码语言:javascript
复制
<%= 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 %>

这些是我的控制员:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

及相关路线:

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-12 18:31:55

您处于属于类别控制器的视图中。

您需要在类别控制器的显示操作中添加:

代码语言:javascript
复制
# CategoriesController
def show
 @order_item = OrderItem.new # Add this line
 # rest of your code
end

并在您的form_for order_item中替换为@order_item

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37777551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档