【发布时间】:2014-09-03 07:43:31
【问题描述】:
我们(基于 Agile webdev Rails4 一书)构建了一个购物/查询车。
访客可以将line_items(房屋)添加到购物车,然后结帐。当访客结帐时,会创建潜在客户。
我的模特:
class House < ActiveRecord::Base
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :lead
belongs_to :house
belongs_to :cart
end
class Lead < ActiveRecord::Base
has_many :line_items, dependent: :destroy
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
line_items << item
end
end
end
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
accepts_nested_attributes_for :line_items
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
Carts_controller
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
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
end
Line_item 控制器
class LineItemsController < ApplicationController
skip_before_action :authorize, only: :create
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def create
house = House.find(params[:house_id])
@line_item = @cart.line_items.build(house_id: house.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart,
notice: 'Vakantiehuis toegevoegd in lijst.' }
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
leads_controller
class LeadsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
def create
@lead = Lead.new(lead_params)
@lead.add_line_items_from_cart(@cart)
respond_to do |format|
if @lead.save
format.html { redirect_to @lead, notice:
'Thank you for your order.' }
format.json { render action: 'show', status: :created,
location: @order }
else
format.html { render action: 'new' }
format.json { render json: @lead.errors,
status: :unprocessable_entity }
end
end
end
end
我们想添加一个新模型(将公寓添加到购物车)。所以我在 line_items 表中添加了 apartment_id 并更改了模型
class Apartment < ActiveRecord::Base
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :lead
belongs_to :house
belongs_to :apartment
belongs_to :cart
end
但我现在不知道如何更改 LineItems_controller 中的 create 方法,以便我们可以将房屋和公寓添加到购物车?
谢谢雷姆科
【问题讨论】:
标签: ruby-on-rails