如何将我的购物车链接到用户会话?
我对编程非常陌生,我正在尝试让购物车在我的应用程序中工作,该应用程序链接到用户会话。因此,每个用户都可以拥有自己的购物车,并且没有人可以查看其他人的购物车。
感谢 Railscasts,我有一个工作车,但它目前正在自己的会话中创建。因此,如果您以不同的用户身份登录并不重要,只有一个购物车在使用,并且所有用户共享它。
它目前的创建方式如下:
应用程序控制器
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
helper_method :current_cart
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def current_cart
if session[:cart_id]
@current_cart ||= Cart.find(session[:cart_id])
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil?
@current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end
@current_cart
end
end
行项目控制器
class LineItemsController < ApplicationController
def create
@product = Product.find(params[:product_id])
@line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
flash[:notice] = "Added #{@product.name} to cart."
redirect_to current_cart_url
end
end
我已经将 user_id 添加到购物车模型并设置用户 has_one cart 和 cart own_to a用户,但我无法弄清楚创建购物车的方式需要改变什么才能真正发挥作用。
编辑 - 会话控制器
def create
user = User.authenticate(params[:username], params[:password])
if user
session[:user_id] = user.id
current_cart.user = current_user
current_cart.save
redirect_to root_path, :notice => "Welcome back!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, :notice => "Logged out!"
end
非常感谢任何帮助!
Im super new to programming and I'm trying to get a shopping cart working in my app which is linked to user sessions. So each user can have their own shopping cart and no one can view anyone else's cart.
I have a working cart thanks to Railscasts but its currently being created in its own session. So it doesn't matter if you log in as different users, there is only ever one cart in use and all user share it.
Its currently being created as such:
Application controller
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
helper_method :current_cart
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def current_cart
if session[:cart_id]
@current_cart ||= Cart.find(session[:cart_id])
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil?
@current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end
@current_cart
end
end
Line items controller
class LineItemsController < ApplicationController
def create
@product = Product.find(params[:product_id])
@line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
flash[:notice] = "Added #{@product.name} to cart."
redirect_to current_cart_url
end
end
I've got as far as adding a user_id to the cart model and setting user has_one cart and a cart belong_to a user but I can't figure out what needs to change in the way a cart is created to actually get it to work.
edit - Sessions controller
def create
user = User.authenticate(params[:username], params[:password])
if user
session[:user_id] = user.id
current_cart.user = current_user
current_cart.save
redirect_to root_path, :notice => "Welcome back!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, :notice => "Logged out!"
end
Any help is very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
购物车与会话绑定,因此并非所有用户都会共享它,它对于创建它的浏览器会话来说是唯一的 - 本质上,每个创建的访问 LineItemsController#create 方法的浏览器会话都有一个购物车。
这样做通常是为了允许在用户登录或注册之前创建购物车,从而减少实际将商品添加到购物车时的麻烦。
如果您想将购物车与用户关联,那么您可以在他们登录或注册时执行此操作。如果您已经添加了关系,那么这应该很简单:
The cart is tied to the session, so not all users will share it, it will be unique to the browser session it was created in - essentially one cart for every browser session created that visits your LineItemsController#create method.
This is typically done to allow a cart to be created before a user logs in or signs up, reducing the friction in actually adding items to a cart.
If you want to associate the cart with a user, then you could do this when they log in or sign up. If you've added the relation, then this should be as simple as: