App модель: Корзиныhas_many :line_items
Продуктhas_many :line_items
LineItembelongs_to :cart
, belongs_to :product
Рельсы. Неопределенный метод `remove_product» для ноля: NilClass
Если же продукт добавляется в несколько раз корзины, я считаю, что номер в cart.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
действии?
Большое спасибо!
'@ cart' не определен. Если вы настроили его с помощью 'before_action', для этого вам нужно использовать метод' reduce'. – Pavan