Uygulamamda Çevreyolu Agile Web Geliştirme 4 numaralı kitaptaki yerleşime benzer bir format izlenirken bir kart tasarladım ve bir sorunum var. Dolayısıyla, bir kullanıcı bir alışveriş sepetine ürün ekleyebilir, sepetlerini görüntüleyebilir ve her bir öğenin fiyatını ve toplamı görebilir. İçinde bulunduğum konu, bir kullanıcının öğeleri bir sepete koyar ve sonra da çıkış yapar. Sepeti, farklı bir kullanıcı oturum açtığında bile sepetteki eşyaları tutar. Her kullanıcının kendi kişiliğine sahip olmasının uygun bir yol olduğuna inanıyorum. araba. İşte Raylar: Bir kullanıcı oturumuna nasıl bir araba kaydedilir?
benim kullanıcı modeli ve denetleyiciclass 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
Benim Sepeti modeli ve denetleyici olduğunu
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
ve benim Satır Öğeleri denetleyici ve akım modülü (satır öğeleri benim düzeninde bir sepete bir kısmını ilişkilendirir)
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
Tek kullanımla ilişkilendirilecek bir arabayı nasıl alabileceğime dair herhangi bir öneri r büyük bir yardım olurdu :) artık bilgi gerekiyorsa sadece sorun. Tekrar teşekkürler!
yılında İşaret Üzerine Sana oturumu almak için Oturum kullanarak ne anlama tarafından biraz karıştı. MVC oturumlarım var, bir şekilde buna dahil olmam gerekiyor mu demek istiyorsun? – Dan
Evet, bu üç satırlık kodla oturum denetleyicisinde bir arabayı oluşturmayı denedim '@cart = Cart.create user.cart = @cart @ user.save' bu doğru yolda mı? – Dan