New to rails. I have seen several tutorials online, but am unable to get a simple shopping cart functionality. Following is my code and the error i am getting.
class CartsController < ApplicationController
def add
@current_cart = @cart
@current_cart = @current_cart.products.new(product_params)
@current_cart.save
session[:current_cart_id] = @current_cart.id
redirect_to :back
end
def delete
end
def show
#@cart = initialize_cart.cart
end
private
def product_params
params.permit(:id)
end
end
Application Controller
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :initialize_cart
def initialize_cart
if session[:cart]
@cart = Cart.find(session[:cart])
else
@cart = Cart.create
session[:cart] = @cart.id
end
end
end
Link to Add to Cart Button
<%= link_to "Add to Cart", :controller => "carts", :action => "add", :id => @product.id, :class => "btn bg-turquoise"%>
routes.rb
get 'carts/add/:id', to: 'carts#add'
get 'carts/delete'
get 'carts/show', to: 'carts#show', as: 'showcart'
Error:
ActiveModel::UnknownAttributeError in CartsController#add
unknown attribute 'cart_id' for Product.
Extracted source (around line #5):
3 def add
4 @current_cart = @cart
**5 @current_cart = @current_cart.products.new(product_params)**
6 @current_cart.save
7 session[:current_cart_id] = @current_cart.id
Any help or guidance is appreciated. Thanks!
Aucun commentaire:
Enregistrer un commentaire