Rails test failure 500 when I use .includes - ruby-on-rails

I'm developing a small e-commerce and I´ve a problem when I run the tests for my cart controller.
here cart controller
class CartsController < ApplicationController
include CurrentCart #modul current_cart in controllers/concerns
before_action :set_cart, only: [:show, :edit, :update, :destroy]
before_action :set_current_cart, only: [:index]
# GET /carts
# GET /carts.json
def index
respond_to do |format|
format.html { redirect_to #cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: #line_item }
end
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
#cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: #cart }
else
format.html { render :new }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if #cart.update(cart_params)
format.html { redirect_to #cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: #cart }
else
format.html { render :edit }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
#cart.destroy if #cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to catalog_index_url, notice: 'Cart was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
#cart = Cart.includes(line_items: :product).find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
end
and here my tests:
require 'test_helper'
class CartsControllerTest < ActionDispatch::IntegrationTest
setup do
#line_item = line_items(:one)
#product = products(:one)
#cart = carts(:one)
end
test "should get index" do
get carts_url
assert_response :success
end
test "should get new" do
get new_cart_url
assert_response :success
end
test "should create cart" do
assert_difference('Cart.count') do
post carts_url, params: { cart: {} }
end
assert_redirected_to cart_url(Cart.last)
end
test "should show cart" do
get cart_url(#cart)
assert_response :success
end
test "should get edit" do
get edit_cart_url(#cart)
assert_response :success
end
test "should update cart" do
patch cart_url(#cart), params: { cart: {} }
assert_redirected_to cart_url(#cart)
end
test "should destroy cart" do
post line_items_url, params: { product_id: products(:one).id }
#cart = Cart.find(session[:cart_id])
assert_difference('Cart.count', -1) do
delete cart_url(#cart)
end
assert_redirected_to catalog_index_url
end
end
here the results:
Failure: CartsControllerTest#test_should_destroy_cart Expected
response to be a <3XX: redirect>, but was a <500: Internal Server
Error>
Failure: CartsControllerTest#test_should_get_edit Expected response
to be a <2XX: success>, but was a <500: Internal Server Error>
Failure: CartsControllerTest#test_should_update_cart Expected
response to be a <3XX: redirect>, but was a <500: Internal Server
Error>
here is the modul current cart for the cartsController:
module CurrentCart
private
def set_current_cart
#cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
#cart = Cart.create
session[:cart_id] = #cart.id
end
end
I think that the problem is here:
if I remove this in the method set_cart from the cart controller: .includes(line_items: :product) the tests will run perfectly without failure, but I cannot understand why.
(I put the include because otherwise I get this error when I access the cart from the browser:
USE eager loading detected LineItem => [:product] Add to your
finder: :includes => [:product] Call stack
UPDATE:
I found something interesting in the file "test.log" :
Bullet::Notification::UnoptimizedQueryError - user: claudio
DELETE /carts/980190963
AVOID eager loading detected
LineItem => [:product]
Remove from your finder: :includes => [:product]
Call stack
##here there's a path##.rb:45:in `block (2 levels) in <class:CartsControllerTest>'
##here there's a path##:in `block in <class:CartsControllerTest>'

Related

Rails - How to pass controller tests when I use before_action

I need to run some tests and I have come at a stand still here.
I am using before_action in my Appointments controller
Here is the controller
class AppointmentsController < ApplicationController
before_action :set_appointment, only: %i[ show edit update destroy ]
#before we run anything if the user is not signed in show index and show functions
before_action :authenticate_user!, except: [:index,:show]
#only the correct user can edit,update and destroy
before_action :correct_user, only: [:edit, :update , :destroy]
# GET /appointments or /appointments.json
def index
#appointments = Appointment.all.decorate
end
# GET /appointments/1 or /appointments/1.json
def show
end
# GET /appointments/new
def new
##appointment = Appointment.new
#appointment = current_user.appointments.build
end
# GET /appointments/1/edit
def edit
end
#function to allow for search functionality
def search
#appointments = Appointment.where("date LIKE?", "%"+params[:q]+"%")
end
# POST /appointments or /appointments.json
def create
##appointment = Appointment.new(appointment_params)
#appointment = current_user.appointments.build(appointment_params)
#here underneath I am using my custom gem to filter bad words within the notes field when creating an appointment
#appointment.notes = Badwordgem::Base.sanitize(#appointment.notes)
respond_to do |format|
if #appointment.save
format.html { redirect_to appointment_url(#appointment), notice: "Appointment was successfully created." }
format.json { render :show, status: :created, location: #appointment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /appointments/1 or /appointments/1.json
def update
respond_to do |format|
if #appointment.update(appointment_params)
format.html { redirect_to appointment_url(#appointment), notice: "Appointment was successfully updated." }
format.json { render :show, status: :ok, location: #appointment }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /appointments/1 or /appointments/1.json
def destroy
#appointment.destroy
respond_to do |format|
format.html { redirect_to appointments_url, notice: "Appointment was successfully destroyed." }
format.json { head :no_content }
end
end
#function here that restricts editing so the current logged in user can edit only their records
def correct_user
#appointment = current_user.appointments.find_by(id: params[:id])
redirect_to appointments_path, notice:"NOT ALLOWED TO EDIT THIS" if #appointment.nil?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
#appointment = Appointment.find(params[:id])
end
# Only allow a list of trusted parameters through.
def appointment_params
params.require(:appointment).permit(:barber, :customer, :notes, :date,:user_id)
end
end
and here is my test controller
require "test_helper"
class AppointmentsControllerTest < ActionDispatch::IntegrationTest
Devise::Test::IntegrationHelpers
setup do
#appointment = appointments(:one)
end
test "should get index" do
get appointments_url
assert_response :success
end
test "should get new" do
get new_appointment_url
assert_response :success
end
test "should create appointment" do
assert_difference('Appointment.count') do
post appointments_url, params: { appointment: { barber: #appointment.barber, customer: #appointment.customer, date: #appointment.date, notes: #appointment.notes } }
end
assert_redirected_to appointment_url(Appointment.last)
end
test "should show appointment" do
get appointment_url(#appointment)
assert_response :success
end
test "should get edit" do
get edit_appointment_url(#appointment)
assert_response :success
end
test "should update appointment" do
patch appointment_url(#appointment), params: { appointment: { barber: #appointment.barber, customer: #appointment.customer, date: #appointment.date, notes: #appointment.notes } }
assert_redirected_to appointment_url(#appointment)
end
test "should destroy appointment" do
assert_difference('Appointment.count', -1) do
delete appointment_url(#appointment)
end
assert_redirected_to appointments_url
end
end
If I comment out the "before actions" in my controller , of course all the tests pass but with them 15 tests fail.
How do I make the tests pass with the before_action ?
For :authenticate_user just use the helper log_in in te test, after create a user, like this:
class AppointmentsControllerTest < ActionDispatch::IntegrationTest
let(:user) { User.new(user_params) }
......
and put
sign_ig user
inside each 'it methods you want'
For :set_appointment or :correct_user just passing right id params inside the path's call

Ruby on Rails: Error when trying to add item to cart (using line items)

I have three models:
Tracks, LineItems and Cart
By using line item associations, I'm adding tracks (for purchase) to a cart. Yet when I click 'add to cart' the following error message gets thrown:
No route matches [POST] "/line_items/1"
Despite extensively reviewing my code; I can't seem to find the problem, is it in my controller for line_items? It appears as though it is trying to post a line_item with id = 1 which doesn't exist?
line_items_controller.rb
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
before_action :set_cart, only: [:create]
# 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
#track = Track.find(params[:track_id])
#line_item = #cart.add_track(track)
respond_to do |format|
if #line_item.save
format.html { redirect_to #line_item, 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
# 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
#cart = Cart.find(session[:cart_id])
#line_item.destroy
respond_to do |format|
format.html { redirect_to cart_path(#cart), notice: 'Line item was successfully destroyed.' }
format.json { head :no_content }
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(:track_id)
end
end
carts_controller.rb
class CartsController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with :invalid_cart
before_action :set_cart, only: [:show, :edit, :update, :destroy]
# GET /carts
# GET /carts.json
def index
#carts = Cart.all
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
#cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: #cart }
else
format.html { render :new }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if #cart.update(cart_params)
format.html { redirect_to #cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: #cart }
else
format.html { render :edit }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
#cart.destroy if cart.id == session[:cart_id] #hook into current client session instead of user
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to root-path, notice: 'Cart was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
#cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to root_path, notice: "That cart doesn't exist"
end
end
views > carts > show.html.haml
.keep-shopping.pv1.mt4.has-text-right
= link_to 'Keep Shopping', tracks_path, class: 'button is-warning'
%hr/
%section.section
= render(#cart.line_items)
.columns
.column
= button_to 'Empty Cart', #cart, method: :delete, data: { confirm: "Are you sure? " }, class: "button is-danger"
.column.total.has-text-right
%h4.title.is-4
%span.f5.has-text-grey Total:
= number_to_currency(#cart.total_price)
views > _line_items.html.haml (line helper partial)
.columns.align-items-center
.column.is-1
= line_item.quantity
.column.is-2
%figure.is-128x128.image
= image_tag(line_item.track.image_url(:thumb))
.column.is-9
%strong= line_item.track.name
.columns.align-items-center
.content.column.is-9
= truncate(line_item.track.description, length: 140)
.column.is-3.has-text-right
%strong.f4= number_to_currency(line_item.total_price)
.has-text-right
= link_to 'Remove Item', line_item, method: :delete, data: { confirm: "Are you sure? " }, class: "button is-small mb4"
= succeed "/" do
%hr/
I have come across this issue before. I am most definitely from your "add to cart button". I suppose your LineItem belongs_to The Cart and the product. If so when you're passing your button data to your line_item "add to cart" button, you might wanna set the current cart also so that you send over both IDs.
If possible you might want to share more code snippets of your button and your relationships.

No route matches {:action=>"show", :controller=>"carts", :id=>nil}, missing required keys: [:id]

I am trying to create a simple add to cart with Ruby on Rails and I having an issue to display a simple link to cart that has the last id of the user that just added an item to the cart.
Simple I getting this error:
No route matches {:action=>"show", :controller=>"carts", :id=>nil}, missing required keys: [:id]
This is my link to the cart and it the place where I am getting the error
<%= link_to 'Cart', cart_path(#cart), class: 'header__cart_link' %>
Routs look same as they should be, I am calling it
resources :carts
In console rake routes
carts GET /carts(.:format) carts#index
POST /carts(.:format) carts#create
new_cart GET /carts/new(.:format) carts#new
edit_cart GET /carts/:id/edit(.:format) carts#edit
cart GET /carts/:id(.:format) carts#show
PATCH /carts/:id(.:format) carts#update
PUT /carts/:id(.:format) carts#update
DELETE /carts/:id(.:format) carts#destroy
Also, I am using a line_items controller to create a reference for the cart so that I making a relation of the product and cart in the line_items
class CartsController < ApplicationController
before_action :set_cart, only: %i[show edit update destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
load_and_authorize_resource
def index
#carts = Cart.all
end
def new
#cart = Cart.new
end
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: #cart }
else
format.html { render :new }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #cart.update(cart_params)
format.html { redirect_to #cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: #cart }
else
format.html { render :edit }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
def destroy
# #cart.destroy if #cart.id == session[:cart_id]
#cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to root_path, notice: 'Cart was successfully emptied.' }
format.json { head :no_content }
end
end
def empty_cart
if !#cart.nil?
redirect_to cart_path(#cart)
else
redirect_to cart_path(session[:cart_id])
end
end
private
def set_cart
#cart = Cart.find(params[:id])
end
def cart_params
params.fetch(:cart, {})
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to root_path, notice: 'Invalid cart'
end
end
and here the cart controller it self
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: #cart }
else
format.html { render :new }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
In my application_controller.rb I handaling the set_cart
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include CurrentCart
before_action :set_cart
# before_filter :configure_permitted_parameters, if: :devise_controller?
before_action :store_session_data
before_action :clean_session_cart
rescue_from CanCan::AccessDenied do |exception|
if user_signed_in?
redirect_to main_app.root_url, alert: exception.message
else
redirect_to new_user_session_path, alert: exception.message
end
end
protect_from_forgery
protected
def clean_session_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
session[:cart_id] = nil
end
def store_session_data
RequestStore.store[:session] = session
end
end
and then I creating a concerns in the models current_cart.rb
module CurrentCart
extend ActiveSupport::Concern
private
def set_cart
begin
#cart = Cart.find(params[:cart_id] || session[:cart_id])
rescue ActiveRecord::RecordNotFound
#cart = Cart.create
end
session[:card_id] = #cart.id
end
end
#cart is not initialized for view/layouts/_header.html.erb.
set cart in your application controller
class ApplicationController < ActionController::Base
before_action :set_global_cart
def set_global_cart
#global_cart = current_user.cart || current_user.cart.create()
//something similar & it will be better to use different name instead of #cart
end
end

RSpec undefined method `request='

I am getting the following error from a RSpec get request:
1) Flight GET/index displays flights
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `request=' for #<Flight:0x00000006a2ad68>
Here is my spec/requests/flights_spec.rb :
require 'rails_helper'
require 'spec_helper'
require 'flight'
RSpec.describe Flight, type: :controller do
describe 'GET/index', :type => :request do
it 'displays flights' do
Flight.create!(:destination => 'San Francisco')
get :index
response.body.should include('San Francisco')
end
end
end
Here is my spec/controllers/flights_controller_spec.rb:
require 'rails_helper'
RSpec.describe FlightsController, type: :controller do
describe 'GET index' do
end
end
Here is my app/controllers/flights_controller.rb:
class FlightsController < ApplicationController
before_action :set_flight, only: [:show, :edit, :update, :destroy]
# GET /flights
# GET /flights.json
def index
#flights = Flight.all
end
# GET /flights/1
# GET /flights/1.json
def show
end
# GET /flights/new
def new
#flight = Flight.new
end
# GET /flights/1/edit
def edit
end
# POST /flights
# POST /flights.json
def create
#flight = Flight.new(flight_params)
respond_to do |format|
if #flight.save
format.html { redirect_to #flight, notice: 'Flight was successfully created.' }
format.json { render :show, status: :created, location: #flight }
else
format.html { render :new }
format.json { render json: #flight.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /flights/1
# PATCH/PUT /flights/1.json
def update
respond_to do |format|
if #flight.update(flight_params)
format.html { redirect_to #flight, notice: 'Flight was successfully updated.' }
format.json { render :show, status: :ok, location: #flight }
else
format.html { render :edit }
format.json { render json: #flight.errors, status: :unprocessable_entity }
end
end
end
# DELETE /flights/1
# DELETE /flights/1.json
def destroy
#flight.destroy
respond_to do |format|
format.html { redirect_to flights_url, notice: 'Flight was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_flight
#flight = Flight.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def flight_params
params.require(:flight).permit(:departure, :arrival, :destination, :baggage_allowance, :capacity)
end
end
Not sure what's gong on - any help appreciated - thanks in advance,
Slavko
At the top of your spec you've got
RSpec.describe Flight, type: :controller
But then just below
describe "GET /index", type: :request
You've got to pick one or the other (from what's in the spec you probably want a controller spec)
In addition for a controller spec the described object should be a controller class, not a model class.

Assigning Carts to User

I'm trying to build a shopping cart that is assigned to a user. What happens now though is when an item is added to the shopping cart, it's added for every user. It seems every user is using the same cart and I can't get users from being able to add to to other users carts. How can I make it so that each user has a unique cart and can only add to theirs?
Here's what I have:
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :edit, :update, :destroy, :show]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
# GET /carts
# GET /carts.json
def index
#carts = Cart.all
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
#cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render action: 'show', status: :created, location: #cart }
else
format.html { render action: 'new' }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if #cart.update(cart_params)
format.html { redirect_to #cart, notice: 'Cart was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
#cart.destroy if #cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url, notice: "Your cart is currently empty" }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
#cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params[:cart]
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: "Invalid Cart"
end
end
current_cart.rb
module CurrentCart
extend ActiveSupport::Concern
private
def set_cart
#cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
#cart = Cart.create
session[:cart_id] = #cart.id
end
end
line_item_controller.rb
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :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
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product: product)
respond_to do |format|
if #line_item.save
format.html { redirect_to #line_item.cart, notice: 'Line item was successfully created.' }
format.json { render action: 'show', status: :created, location: #line_item }
else
format.html { render action: '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.cart, notice: 'Line item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: '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.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.json { head :no_content }
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
Instead of Cart.create and Cart.find in CurrentCart.rb, you should do something like:
def set_cart
#cart = current_user.carts.find_by(id: session[:cart_id]) || current_user.carts.create
session[:cart_id] = #cart.id
end
find_by doesn't throw an error if it can't find it; it just returns nil, so you don't have to worry about rescuing.

Resources