레일즈 3 에디션으로 애자일 웹 개발을 선택 했으므로 저장소 애플리케이션 챕터를 통해 간단한 수량 편집 기능을 만들고 기능을 삭제하려고합니다. 나는 삭제 기능을 가지고 행운을 얻었지만 수량 편집 기능에는 아무런 운이 없었습니다.간단한 수량 함수 문제, 장바구니
나는 많은 정보를 제공 할 것이므로 압도 당하지 마십시오. 나는 이것이 이것이 어려운 문제라는 것을 알았다.
는 add_to_cart.html.erb하려면
<div class="cart-title">Your cart</div>
<table>
<% for item in @cart.items %>
<tr>
<td><% form_for @cart.items, :url => {:action => "cart_update", :id => "#{item.getinventoryid}"} do |f| %>
<%= f.text_field :quantity, :size => '3' %>
<%= f.hidden_field :id, :value => "#{item.getinventoryid}" %>
<%= f.submit 'cart_update' %>
<% end %></td>
<td><%=h item.quantity %> ×</td>
<td><%=h item.title %></li></td>
<td><%=h item.description %></td>
<td class="item-price"><%= number_to_currency(item.price) %></td>
<td><%= link_to 'remove', {:controller => 'inventories', :action => 'remove_cart_item', :id => "#{item.getinventoryid}"} %></td>
</tr>
<% end %>
<tr class="total-line">
<td colspan="4">Total</td>
<td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to "Checkout", :action => 'checkout' %>
<%= button_to 'Empty cart', :action => 'empty_cart' %>
inventories_controller :
def cart_update
@inventory = Inventory.find(params[:id])
@cart = find_cart
@cart.increment_inventory_quantity(params[:inventory])
end
def remove_cart_item
inventory = Inventory.find(params[:id])
@cart = find_cart
@cart.remove_inventory(inventory)
redirect_to_index("The item was removed")
end
Cart.rb 모델
attr_accessor :items
def increment_inventory_quantity(id, quantity)
inventory_to_increment = @items.select{|item| item.inventory == inventory}
# We do this because select will return an array
unless product_to_increment.empty?
inventory_to_increment = inventory_to_increment.first
else
# error handling here
end
inventory_to_increment.quantity = quantity
end
def remove_inventory(inventory)
@items.delete_if {|item| item.inventory == inventory }
end
cart_item.rb 모델
attr_accessor :inventory, :quantity
def getinventoryid
@inventory.id
end
이 생산하는 이상한 결과 :
공지 사항 16 내 루프에서 두 항목에 표시되는 양 (# 실패). InventoriesController에서 ArgumentError 양식을 제출하면 # cart_update 잘못된 인수 수 (1에 대해 2) 오류가 반환됩니다. 전달되는 매개 변수 :
{"commit"=>"cart_update",
"_method"=>"put",
"authenticity_token"=>"sH1tWXTJPltpSq5XaAkww7259IR5ZiflnqSFB2Zb0IY=",
"id"=>"50",
"cart_item"=>{"quantity"=>"16",
"id"=>"50"}}
안녕 JZ :
은 아마 당신은 이런 식으로 뭔가를 할 의도. 첫째, @inventory가 cart_item 모델에 어떻게 설정되어 있습니까? 둘째, "잘못된 인수 수"오류에 대한 자세한 정보를 제공 할 수 있습니까? 그것은 어떤 방법으로 그 오류를 던지고 있는지를 아는 데 도움이됩니다. 감사! –안녕하세요, Jamie 님, 위 오류를 업데이트했습니다. 이 오류는 cart_update 메소드에서 발생합니다. 아래의 수정은 표시되는 적절한 단위를 수정하는 방식으로 수행되었지만 내 방법은 여전히 잘못된 개수의 인수 오류를 생성합니다. 내 cart_item에서 @Inventory의 측면에서 : 데프 초기화 (재고) \t \t @inventory = 재고 \t \t @quantity = 1 끝에 표시된 수량을 수정 –