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
내 양식 코드를 보여 주시겠습니까 – Choco
안녕하세요 초코 내 양식 코드를 보내주세요. 제발 도와주세요 ... 고마워요. – Zombie
새 방법과 모델을 추가하십시오. – Choco