找不到没有ID构建关联的产品

时间:2014-11-21 00:44:12

标签: ruby-on-rails associations

我在Ruby on Rails上遇到了问题。当我尝试创建一个line_items时,这是产品和购物车的关联..siguiendo使用Rails进行敏捷Web开发。

以下是代码:

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

这是产品型号

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

非常感谢Choco,我做了你发送的并运行

@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]。