アプリケーションモデル: カートhas_many :line_items
製品has_many :line_items
のLineItembelongs_to :cart
、belongs_to :product
Rails。未定義のメソッドはnilのための `remove_product」:NilClass
同じ製品をカートに追加された場合は、複数回、私はそれを数えます番号カート.rb:
def add_product(product)
current_item = line_items.find_by(product_id: product.id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product.id)
end
current_item
end
今度は、その数を増減する機能をユーザーに与えたいと考えています。
line_items_controller.rb:
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_index_url }
format.js { @highlited_item = @line_item }
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
line_item.html.erb:増加うまくいっ
<td><%= link_to "+", line_items_path(product_id: line_item.product), method: :post, remote: true %></td>
しかし、そううまくいかない減少。私の計画は次のとおりです。
cart.rb(これは、どのように動作するかわからない、ここで以来、未定義のメソッドadd_product火災のエラーアップ):
def remove_product(product)
current_item = line_items.find_by(product_id: product.id)
if current_item.quantity > 1
current_item.quantity -= 1
elsif current_item.quantity = 1
current_item.destroy
end
current_item unless current_item.nil
end
line_items_controller.rb:
def decrease
product = Product.find(params[:product_id])
@line_item = @cart.remove_product(product)
end
完全line_items_controller.rb:
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def index
@line_items = LineItem.all
end
def show
end
def new
@line_item = LineItem.new
end
def edit
end
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_index_url }
format.js { @highlited_item = @line_item }
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
def update
respond_to do |format|
if @line_item.update(line_item_params)
format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
format.json { render :show, status: :ok, location: @line_item }
else
format.html { render :edit }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
def destroy
@line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id)
end
end
remove_product
の内容がdecrease
の理由は何ですか?
多くの感謝!
'@カート 'は定義されていません。 'before_action'を使ってそれを設定した場合、そのための' reduce'メソッドが必要です。 – Pavan