LineItemsController中的NameError #create(未初始化的常量LineItem :: CaterOrderOptions)

时间:2017-02-17 22:42:36

标签: ruby-on-rails ruby-on-rails-5

我跟随着一本名为Agile Web Development with Rails 5的书。在大多数情况下,我使用不同的文件名,所以我可以强迫自己不要简单地复制/粘贴代码。在我尝试实现购物车和line_items之前,一切都进展顺利。

正如标题所述,我在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 / create class

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

1 个答案:

答案 0 :(得分:0)

belongs_to始终采用单数形式,因此请更改此行以引用cater_order_option而不是cater_order_options

belongs_to :cater_order_option