私のアプリケーションでは、本のレイアウトと同様の書式で、カートをデザインしました。アジャイルWeb開発でRails 4と少し問題があります。ユーザーはカートにアイテムを追加したり、カートを表示したり、各アイテムの価格や合計を表示することができます。私が遭遇した問題は、ユーザーがカートにアイテムを入れてからサインアウトしたときに、別のユーザーがサインインしてもカートがアイテムをカート内に保持することです。カート。ここで Rails:カートをユーザーセッションに保存する方法は?
は私のユーザモデルとコントローラclass User < ActiveRecord::Base
has_one :cart
# :confirmable, :lockable, :timeoutable and :omniauthable
# :confirmable, :lockable, :timeoutable and :omniauthable
has_secure_password
validates :email, presence: true
end
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
redirect_to @user
end
end
private
def user_params
params.require(:user).permit(:first_name, :admin, :last_name, :email, :password, :password_confirmation, :phone_number, :address_one, :address_two, :city, :country, :state, :zip)
end
end
マイカート・モデルとコントローラである
class Cart < ActiveRecord::Base
has_many :order_items
belongs_to :user
has_many :line_items, dependent: :destroy
def add_part(part_id)
current_part = line_items.find_by(part_id: part_id)
if current_part
current_part.quantity += 1
else
current_part = line_items.build(part_id: part_id)
end
current_part
end
def total_price
line_items.to_a.sum { |item| item.total_price}
end
end
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def show
@cart = Cart.find(params[:id])
end
def edit
@cart = Cart.new(cart_params)
end
def update
@cart = Cart.find(params[:id])
if @cart.update_attributes(cart_params)
redirect_to @cart
end
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to root_path }
format.json { head :no_content }
end
end
private
def cart_params
params.require(:cart).permit(:user_id)
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to root_path, notice: "Invalid cart"
end
end
と広告申込コントローラと現在のモジュール(ラインアイテムは、私のレイアウトでカートに一部を関連付け)
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def create
part = Part.find(params[:part_id])
@line_item = @cart.add_part(part.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart }
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
end
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
1つのカートを1つの用途に関連付ける方法についてのアドバイスrは大きな助けになるでしょう:)もしもう情報が必要ならば、ただ尋ねてください。再度、感謝します!
サインインで、私はあなたがセッション見るにはサインインして使用することによって何を意味するかによって少し混乱しています。私はセッションを持っているMVCどういうわけかそれを組み込む必要があるのですか? – Dan
さて、私はこの3行のコードでカートのインスタンスを作成しようとしました。 '@cart = Cart.create user.cart = @cart @ user.save' これは正しい軌道にありますか? – Dan