2014-11-21 120 views
0

我有一个Ruby on Rails的问题。当我尝试创建一个line_items是产品和购物车的关联..siguiendo图书敏捷web开发与Rails。找不到产品没有身份证建立联系

下面的代码:

def create 
product = Product.find(params[:product_id]) 
#@line_item = LineItem.new(line_item_params) 
@line_item = @cart.line_items.build(product: product) 

respond_to do |format| 
    if @line_item.save 
    format.html { redirect_to @line_item.cart, 
     notice: 'Line item was successfully created.' } 
    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 

如果我取消注释行:

line_item = LineItem.new(line_item_params) 

和评论

#product = Product.find(params[:product_id]) 
#@line_item = @cart.add_product(product: product) 

将它的工作?

我知道在line_item_params方法,其是定义为创建对象的下一个

def line_item_params 
    params.require(:line_item).permit(:product_id, :cart_id) 
end 

容许参数。

有人可以帮我建立这个?

感谢

这是我对line_items形式的代码,我不能将代码复制,因为我封锁的代码


<%= form_for(@line_item) do |f| %> 
    <% if @line_item.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@line_item.errors.count, "error") %> prohibited this line_item from being saved:</h2> 
<ul> 
    <% @line_item.errors.full_messages.each do |msg| %> 
    <li><%= msg %></li> 
    <% end %> 
    </ul> 
</div> 
<% end %> 
<div class="field"> 
<%= f.label :product_id %><br> 
<%= f.text_field :product_id %> 
</div> 
<div class="field"> 
<%= f.label :cart_id %><br> 
<%= f.text_field :cart_id %> 
</div> 
<div class="actions"> 
<%= f.submit %> 
</div> 
<% end %> 

某些部分这是新方法

def new 
    @line_item = LineItem.new 
end 

这是th E产品型号

class Product < ActiveRecord::Base 
has_many :line_items 
before_destroy :ensure_not_referenced_by_any_line_item 

validates :title, :description, :image_url, presence: true 
validates :price, numericality: {greater_than_or_equal_to: 0.01} 
validates :title, uniqueness: true 
validates :image_url, allow_blank: true, format: { 
    with: %r{\.(gif|jpg|png)\Z}i, 
    message: 'must be a URL for GIF, JPG or PNG Image.' 
} 

validates :title, length: {minimum: 10} 

#para ultimo producto para cache 
def self.latest 
    Product.order(:updated_at).last 
end 

private 
def ensure_not_referenced_by_any_line_item 
    if line_items.empty? 
     return true 
    else 
     errors.add(:base, 'Line Items present') 
     return false 
    end 

end 
end 

这是车型号

class Cart < ActiveRecord::Base 
has_many :line_items, dependent: :destroy 

def add_product(product_id) 
    current_item= line_items.find_by(product_id: product_id) 
    if current_item 
     current_item.quantity +=1 
    else 
     current_item= line_items.build(product_id: product_id) 
    end 
    current_item 
end 
end 

这是LINE_ITEM型号

class LineItem < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :cart 
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 
+0

你能告诉我你的表单代码 – Choco 2014-11-21 03:28:52

+0

巧克力,您好我发送代码为我的表格,请帮我...谢谢 – Zombie 2014-11-21 15:18:57

+0

可以请您添加'new'方法和您的机型也 – Choco 2014-11-21 16:04:18

回答

0

改变这一点,尝试:

def create 
@cart = Cart.find(params[:line_item][:cart_id]) 
@product = Product.find(params[:line_item][:product_id]) 
@line_item = @cart.line_items.build(product: @product) 

respond_to do |format| 
    if @line_item.save 
    format.html { redirect_to @line_item.cart, 
     notice: 'Line item was successfully created.' } 
    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 
+0

非常感谢Choco, 我还有最后一个问题,使用创建合作关系的正确方法是什么。 1)与所述呼叫的方法: params.require(:LINE_ITEM).permit(:,:PRODUCT_ID cart_id) 2)或作为你提出我与阵列[:LINE_ITEM] [:PRODUCT_ID] 再次非常感谢乔科 – Zombie 2014-11-24 13:33:30

+0

@僵尸请接受我的答案,如果它会对你有所帮助 – Choco 2014-11-25 04:14:09

+0

谢谢乔科我接受你的答案 – Zombie 2014-11-25 21:43:08

0

感谢很多巧克力,我做你送什么,并运行

@cart = Cart.find(params[:line_item][:cart_id]) 
@product = Product.find(params[:line_item][:product_id]) 
@line_item = @cart.line_items.build(product: @product) 

的阵列[:LINE_ITEM] [:cart_id]。