First argument in form cannot contain nil or be empty in a rails form -
i getting mad cause have situation trying modify quantity of item via custom action , don´t know why works in 1 view doesn´t in another, stupid can´t find reason, action add_item works perfectly, update_item_quantity doesn´t display de view , shows exception:
first argument in form cannot contain nil or empty
here code:
routes.rb:
resources :designs resources :carts resources :cart_items :add_to_cart, on: :member post :add_item, on: :member post :update_item_quantity, on: :member end
te controllers following:
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 active cart" redirect_to cart_path(@cart) else flash[:danger] = "design couldn´t added 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
and views are:
/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 button below if want add design cart have active @ moment</p> <%= form_for @cart_item, url: {controller: 'cart_items', action: "add_item", params: {design_id: @design.id}} |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 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>
and here /carts/show.html.erb
<h2>cart, <%= @cart.name %></h2> <div class="col-md-8 tdp-listing cart-content-listing"> <% @cart_items.each |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}} |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 sure want delete item?" }, class: "btn btn-xs btn-danger" %> </div> </div> <% end %> </div>
wait news , in advance.
antonio
please add code cartscontroller#show
action whole exception message too.
i guess have not assigned value instance variable @cart_items
in show
action. that's why <% @cart_items.each |ci| %>
on line 4 failing assign value ci
in /carts/show.html.erb
template.
this should reason why form throwing exception containing nil
value.
Comments
Post a Comment