2014-09-03 84 views
1

我们建立了一个购物车/询价车(基于本书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方法,所以我们可以房屋和公寓添加到购物车?

感谢REMCO

回答

0

你要改变你的line_items模型有一个polymorphic association

enter image description here

Polymorphic associations只是当你有不同的对象在同一个模型相关联的能力。这不会是正确的,如果你apartments是在House模型的子对象,但因为它们是独立的,你就可以给他们多态

这是我怎么会做它相关联:

#app/models/line_item.rb 
class LineItem < ActiveRecord::Base 
    belongs_to :item, polymorphic: true 
end 

#app/models/house.rb 
class House < ActiveRecord::Base 
    has_many :line_items, as: :items 
end 

#app/models/apartment.rb 
class Apartment < ActiveRecord::Base 
    has_many :line_items, as: :items 
end 

这会给你有能力做以下:

#app/controllers/line_items.controller.rb 
class LineItemsController < ApplicationController 
    before_action :create_object 

    def create 
     @line_item = @cart.line_items.build item: @object 
     @line_items.save 
    end 

    private 

    def create_object 
     id = params[:house_id] || params[:apartment_id] 

     model = "House" if params[:house_id] 
     model = "Apartment" if params[:apartment_id] 
     model. constantize 

     @object = model.find id 
    end 
end 

这应该引用正确的模型,您无论是公寓或房子保存到LineItem模型的能力

+0

谢谢...富有...我要改变...让你知道结果是什么! – Remco 2014-09-03 11:20:59

+0

我尝试过,但我得到错误“未定义的方法'找到”房子“:字符串”这是代码:def create_object id = params [:house_id] || PARAMS [:appartment_id如果PARAMS 模型= “豪斯医生”[:house_id] 模型= “公寓” 如果PARAMS [:apartment_id] model.constantize @object = model.find.id 结束 – Remco 2014-09-03 20:26:38

+0

我认为最后一行不正确。@object = model.find.id – Remco 2014-09-03 20:31:27