2016-07-15 18 views
0

私はカスタムアクションでアイテムの数量を変更しようとしている状況があります。別の1つのビューdoesn'tで、おそらくそれは愚かなものですが、私は理由を見つけるcan't、アクションADD_ITEMは完璧に動作しますが、でも表示・デ・ビューをdoesn'tと、この例外を示しupdate_item_quantity:フォームの最初の引数にはnilを含めることはできませんし、レールの形で空にすることはできません

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> 

あなたのニュースをお待ちしており、ありがとうございます。

アントニオ

+0

あなたの 'carts/show.html.erb'で' form_for @ cart_item'を 'form_for ci'に変更してください。 –

+0

soooooooありがとう、それは完全に働いた。私はあなたに1つ、大きなものに借りています。 –

答えて

0

CartsController#showアクションだけでなく、あまりにも全体の例外メッセージのためのコードを追加してください。

アクションでインスタンス変数@cart_itemsに値を割り当てていないと思います。だからこそ、<% @cart_items.each do |ci| %>の4行目は、/carts/show.html.erbテンプレートのciに値を割り当てられません。

これは、nil値を含むフォームが例外をスローする理由です。

関連する問題