我跟着一本名爲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
謝謝你的回覆!我實際上已經嘗試過改變這一點,並再次這樣做。它提示我出現了一個新錯誤,「LineItemsController#create中的ActiveModel :: UnknownAttributeError」。我認爲這可能意味着最後沒有(s)是不正確的,但我想這不是問題?任何想法可能會造成這種情況? – MrJesus
我想它似乎不喜歡我的創建類和前兩行仍然。在似乎至少認識到'cater_order_options_屬性。 – MrJesus
你得到了什麼未知的屬性 – Iceman