0
我生气了,因为我有一种情况,我试图通过自定义操作修改项目的数量,我不知道为什么有用在另一doesn't一种观点认为,这可能是一些愚蠢的,但我不能找到原因,行动的工作方法add_item完美,但update_item_quantity doesn't甚至显示德图,它表示此异常:表单中的第一个参数不能包含零或以空格形式出现在导轨中
First argument in form cannot contain nil or be empty
这里是我的代码:
routes.rb:
resources :designs
resources :carts
resources :cart_items do
get :add_to_cart, on: :member
post :add_item, on: :member
post :update_item_quantity, on: :member
end
个
特控制器如下:
cart_items_controller.rb
def add_item
@cart_item = CartItem.new(add_item_params)
@design = Design.find(params[:design_id])
@cart = Cart.find(current_user.cart_id)
@cart_item.design_id = @design.id
@cart_item.cart_id = current_user.cart_id
if @cart_item.save
flash[:success] = "Design succesfully added to active cart"
redirect_to cart_path(@cart)
else
flash[:danger] = "Design couldn´t be added to your cart"
redirect_to designs_path
end
end
def update_item_quantity
@cart = Cart.find(current_user.cart_id)
@cart_item= CartItem.find(params(:cart_item_id))
redirect_to cart_path(@cart)
end
private
def add_item_params
params.require(:cart_item).permit(:quantity)
end
和意见是:
/cart_items/add_to_cart.html.erb:
<div class="container body-content">
<div class="row col-md-12">
<p>Add design: <%= @design.name %></p>
</div>
<% if current_user.cart_active? %>
<div class="col-md-4 tdp-add-to-cart-panel">
<p>Click on the button below if you want to add this design to the cart you have active at this moment</p>
<%= form_for @cart_item, url: {controller: 'cart_items', action: "add_item", params: {design_id: @design.id}} do |f| %>
<span class="">Qty: </span>
<span class=""><%= f.number_field (:quantity, min: '1', max: '100', step: '1') %></span>
<span class=""><%= f.submit "Add design", class: "btn btn-login" %></span>
<% end %>
<%#= button_to "Add to current cart", {:controller => :carts, :action => :add_item_to_active_cart, :design_id => @design.id }, class: "btn-lg btn-login" %>
</div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<% else %>
<div class="col-md-6"></div>
<div class="col-md-6"></div>
<% end %>
</div>
这里是/carts/show.html.erb
<h2>Cart, <%= @cart.name %></h2>
<div class="col-md-8 tdp-listing cart-content-listing">
<% @cart_items.each do |ci| %>
<div class="row tdp-listing-row">
<div class="col-md-6">
<%= ci.design.name %>
</div>
<div class="col-md-2">
<%= ci.design.price * ci.quantity %>
</div>
<div class="col-md-3">
<%= form_for @cart_item, url: {controller: "cart_items", action: "update_item_quantity", params: {cart_item_id: ci.id}} do |f| %>
<span class="">Qty: ci.id <%= ci.id %> </span>
<span class=""><%#= f.number_field(:quantity, min: '1', max: '100', step: '1') %></span>
<span class=""><%#= f.submit "Update", class: "btn btn-login" %></span>
<% end %>
</div>
<div class="row col-md-1">
<%= link_to "Delete", cart_item_path(ci), method: :delete,
data: { confirm: "Are you sure that you want to delete this item?" }, class: "btn btn-xs btn-danger" %>
</div>
</div>
<% end %>
</div>
等待您的消息,并提前致谢。
安东尼
在'carts/show.html.erb'中将'form_for @ cart_item'改成'form_for ci'。 –
谢谢sooooooo,它运作完美。我欠你一个,还有一个很大的。 –