2014-11-21 4 views
0

Ruby on Rail에 문제가 있습니다. 제품 및 장바구니의 연관성을 나타내는 line_items를 만들려고 할 때 레일스의 Agile 웹 개발 안내서를 참조하십시오.ID가없는 제품을 만들 수 없습니다.

line_item = LineItem.new(line_item_params) 

하고

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

그것을 작동합니다 코멘트 : 나는 행의 주석을 해제하는 경우

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 

: 여기

코드인가?

는 내가 객체를 생성하기 위해 정의 다음

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

허용 매개 변수입니다 line_item_params 방법을 알고.

누군가 나를 만들 수 있습니까?

감사

이 내가 코드


<%= 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 %> 

의 특정 부분을 차단하기 때문에 나는이 새로운 방법의 코드를 복사 할 수 있으며, line_items에 대한 내 양식 코드입니다

def new 
    @line_item = LineItem.new 
end 

이것은 th입니다. 전자 제품 모델

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 

이이이 LINE_ITEM 모델

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

카트입니다 그리고 이것은

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

+0

안녕하세요 초코 내 양식 코드를 보내주세요. 제발 도와주세요 ... 고마워요. – Zombie

+0

새 방법과 모델을 추가하십시오. – Choco

답변

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, 나는 하나의 마지막 질문, 파트너십의 생성을 사용하는 올바른 방법이 될 것입니다. params.require (: LINE_ITEM) .permit (: 메소드의 호출과 1) PRODUCT_ID ,: cart_id) 2) 또는 어레이로 날 제기 같이 : LINE_ITEM] : PRODUCT_ID] 또 많은 감사 Choco – Zombie

+0

@ Zombie 당신이 도움이된다면 제 대답을 받아주십시오 – Choco

+0

감사합니다 Choco 당신의 대답을 받아들입니다. – Zombie

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].

관련 문제