Witam, bardzo proszę o pomoc. Uczę się Rails z pomocą książki Agile Web Development with Rails 5 i napotkałem na problem. Za zadanie mam sprawić aby produkt z koszyka się usunął. Myślałem, że będzie to proste jednak się przeliczyłem.
błąd jaki mi wyskakuje to:
Error:
LineItemsControllerTest#test_should_destroy_line_item:
ActiveRecord::RecordNotFound: Couldn't find LineItem with 'id'=980190962 [WHERE "line_items"."cart_id" = ?]
app/controllers/line_items_controller.rb:62:in `destroy'
test/controllers/line_items_controller_test.rb:58:in `block (2 levels) in <class:LineItemsControllerTest>'
test/controllers/line_items_controller_test.rb:57:in `block in <class:LineItemsControllerTest
Akcja w kontrolerze:
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create, :destroy, :decrement]
before_action :set_line_item, only: [:show, :edit, :update, :destroy, :decrement]
# 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
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product)
respond_to do |format|
if @line_item.save
session[:counter] = 0
format.html { redirect_to store_index_url }
format.js { @current_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
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
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
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
@line_item = @cart.line_items.find(params[:id])
@line_item.destroy
respond_to do |format|
format.html { redirect_to store_index_url }
format.json { head :no_content }
end
end
# POST /line_items/1
# POST /line_items/1.json
def decrement
@line_item.decrement
respond_to do |format|
if @line_item.save
format.html { redirect_to store_index_url }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
@line_item = LineItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_item_params
params.require(:line_item).permit(:product_id)
end
end
Test:
require 'test_helper'
class LineItemsControllerTest < ActionDispatch::IntegrationTest
setup do
@line_item = line_items(:one)
end
test "should get index" do
get line_items_url
assert_response :success
end
test "should get new" do
get new_line_item_url
assert_response :success
end
test "should create line_item" do
assert_difference('LineItem.count') do
post line_items_url, params: { product_id: products(:ruby).id }
end
follow_redirect!
assert_select 'h2', "Your Cart"
assert_select 'td', "Programming Ruby 1.9"
end
test "should create line_item via ajax" do
assert_difference('LineItem.count') do
post line_items_url, params: { product_id: products(:ruby).id },
xhr: true
end
assert_response :success
assert_select_jquery :html, '#cart' do
assert_select 'tr#current_item td', /Programming Ruby 1.9/
end
end
test "should show line_item" do
get line_item_url(@line_item)
assert_response :success
end
test "should get edit" do
get edit_line_item_url(@line_item)
assert_response :success
end
test "should update line_item" do
patch line_item_url(@line_item), params: { line_item: { product_id: @line_item.product_id } }
assert_redirected_to line_item_url(@line_item)
end
test "should destroy line_item" do
assert_difference('LineItem.count', -1) do
delete line_item_url(line_items(:o1_coffee))
end
assert_redirected_to store_index_url
end
end
fixtury line_items.yml:
one:
product: two
cart: one
price: 12
two:
product: two
cart: two
price: 15
o1_coffee:
id: 1
product_id: 1
cart_id: 1
quantity: 2
price: 36.00
o1_ruby:
product_id: 2
cart_id: 1
quantity: 2
price: 49.95
o1_rails:
product_id: 3
cart_id: 1
quantity: 2
price: 34.95
koszyk ustawiam w controllers/concerns/current_cart.rb:
module CurrentCart
private
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
end
Wg. mnie problem jest w przekazywaniu z test_controller destroy parametru sesji dla cart_id. Co bym nie zrobił nie potrafię tego przekazać.