2017-02-17 53 views
0

我跟着一本名为Agile Web Development with Rails 5的书。在大多数情况下,我使用不同的文件名,所以我可以强制自己不要简单地复制/粘贴代码。在我尝试实施购物车和line_items之前,一切都很顺利。LineItemsController中的NameError#create(未初始化的常量LineItem :: CaterOrderOptions)

正如标题所述,我收到一个错误,指出“LineItemsController#create(未初始化的常量LineItem :: CaterOrderOptions)中的NameError”。我的创建类在line_items_controller似乎导致问题。我不太明白这个错误,请让我知道,如果你有任何建议!将不胜感激!

我试着按照我的line_items_controller的书籍语法,不知道我搞砸了什么。本书的格式:

Product.find(params[:product_id]) 
@line_item = @cart.line_items.build(product: product) 

respond_to do |format| 
if @line_item.save 
    format.html { redirect_to @line_item.cart" 

我line_items控制器W /创建一流

class LineItemsController < ApplicationController 
    include CurrentCart 

    before_action :set_cart, only: [:create] 

before_action :set_line_item, only: [:show, :edit, :update, :destroy] 


# GET /line_items 

# GET /line_items.json 

def index 

@line_items = LineItem.all 

end 



# GET /line_items/1 

# GET /line_items/1.json 

def show 

end 


# GET /line_items/new 

def new 

@line_item = LineItem.new 

end 


# GET /line_items/1/edit 

def edit 

end 


# POST /line_items 

# POST /line_items.json 

def create 

cater_order_options = CaterOrderOption.find(params[:cater_order_options_id])  # SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS 


@line_item = @cart.line_items.build(cater_order_options: cater_order_options) # SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS# SOURCE OF PROBLEMS 


respond_to do |format| 
    if @line_item.save 
    format.html { redirect_to @line_item.cart, 
    notice: 'Line item was successfully created.' } 
    format.json { render :show, status: :created, location: @line_item } 
    else 
    format.html { render :new } 
    format.json { render json: @line_item.errors, status: :unprocessable_entity } 
    end 
end 

end 

按钮添加到购物车

<% @CateringMenu.each do |f| %> 
    <tr> 
    <td><%= f.cateringOptions %></td> 
    <td><%= f.CaterDesc %></td> 
    <td><%= f.sideOptions %></td> 
    <td><%= f.sideDesc %></td> 
    <td><%= number_to_currency(f.price) %></td> 
    <td><%= button_to 'Add to Cart', line_items_path(cater_order_options_id: f) %></td> 
    </tr> 
<% end %> 

LINE_ITEM模型

class LineItem < ApplicationRecord 
belongs_to :cater_order_options 
belongs_to :cart 
end 

cater_order_options模型

class CaterOrderOption < ApplicationRecord 
has_many :line_items 
before_destroy :ensure_not_referenced_by_any_line_item 

private 
    #Check to ensure no line items are referencing this product 
    def ensure_not_referenced_by_any_line_item 
     unless line_items.empty? 
      errors.add(:base, 'Line Items Present') 
      #if aborted row is not destroyed 
      throw :abort 
     end 
    end 
end 

cater_order_options控制器 - 它的一些(对变量名)

before_action :set_cater_order_option, only: [:show, :edit, :update, :destroy] 

def index 
    @cater_order_options = CaterOrderOption.all 
end 

def new 
    @cater_order_option = CaterOrderOption.new 
end 

private 

def set_cater_order_option 
    @cater_order_option = CaterOrderOption.find(params[:id]) 
end 


def cater_order_option_params 
    params.require(:cater_order_option).permit(:cateringOptions, :CaterDesc, :sideOptions, :sideDesc, :price) 
end 

回答

0

belongs_to始终以一个单一的,所以改变这一行指的cater_order_option,而不是cater_order_options

belongs_to :cater_order_option 
+0

谢谢你的回复!我实际上已经尝试过改变这一点,并再次这样做。它提示我出现了一个新错误,“LineItemsController#create中的ActiveModel :: UnknownAttributeError”。我认为这可能意味着最后没有(s)是不正确的,但我想这不是问题?任何想法可能会造成这种情况? – MrJesus

+0

我想它似乎不喜欢我的创建类和前两行仍然。在似乎至少认识到'cater_order_options_属性。 – MrJesus

+0

你得到了什么未知的属性 – Iceman

相关问题