【问题标题】:NameError in LineItemsController#create (uninitialized constant LineItem::CaterOrderOptions)LineItemsController#create 中的 NameError(未初始化的常量 LineItem::CaterOrderOptions)
【发布时间】:2017-02-17 22:42:36
【问题描述】:

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

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

我尝试完全按照我的 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

【问题讨论】:

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


    【解决方案1】:

    belongs_to 总是采用单数形式,因此将此行更改为引用 cater_order_option 而不是 cater_order_options

    belongs_to :cater_order_option
    

    【讨论】:

    • 感谢您的回复!我实际上尝试过早一点改变它,然后又做了一次。它提示我一个新错误,“LineItemsController#create 中的 ActiveModel::UnknownAttributeError”。我认为这可能意味着最后没有(s)是不正确的,但我想这不是问题吗?知道是什么原因造成的吗?
    • 我想它似乎不喜欢我的 create 类,前两行仍然。在它似乎至少识别出 'cater_order_options_ 属性之前。
    • 你得到了什么未知属性
    • 它在说“LineItem 的未知属性 'cater_order_options'”。然后,我尝试更改该属性以匹配 line_item 模型中的单数“cater_order_option”,因此我将其更改为“cater_order_option:cater_order_options”(“cater_order_options”与我的代码中看到的变量相同)。这样做之后,我收到一条错误消息,说“无法写入未知属性 'cater_order_option_id'。我需要的属性是“cater_order_options_id”,所以我猜没有(s)是错误的举动?它在我的日志中获取 cater_order_options_id 就好了.
    • 我只能通过在其中包含 line_items db 模式的属性来修复此错误,并且这样做只会导致成功,因为它会提示 jquery 错误消息“必须存在餐饮订单选项”。在书中,数据库在 line_items 数据库模式中没有“产品”,但它仍然使用“line_items.build(product: product)”成功构建了新选择的 line_item。
    猜你喜欢
    • 2015-01-19
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2015-03-01
    • 2018-06-26
    相关资源
    最近更新 更多