Form_for通过params提交数据但控制器没有收到它--Rails 4

时间:2015-11-04 04:06:45

标签: ruby-on-rails ruby-on-rails-4

我是rails的新手并在rails 4中为购物车编写代码。我正在尝试显示添加到购物车中的所有项目,每个项目都在自己的行中。作为显示数据的一部分,我希望有一个字段显示订购商品的数量,并让用户更新数量。目前,表单提交并呈现我可以在调试中看到的哈希,但数据未在数据库中更新。看起来我的控制器操作没有收到数据。

查看

<body>
<div class="container">
<h1>Your Cart</h1>

<br>
<div class= "cart">

  <% if !@cart_item.nil? %>
    <% @cart_item.each do |cart_item| %>
    <row>
      <% product = Product.find(cart_item.product_id) %>
      <div class="col-xs-2">
      <%= link_to image_tag(product.picture.url(:cart)), product_path(product) %>
      </div>
      <div class="col-xs-3">
      <%= product.name %>
      </div>
      <div class="col-xs-1">
      <%= cart_item.quantity %>
      </div>
      <div class="col-xs-1">
      <%= form_for (cart_item) do |f| %>
        <%= f.label :quantity %>
        <%= f.number_field :quantity, class: 'form-control' %>
        <%= f.submit "Update", :controller => 'cart_item', :action => 'quantity_update',class: "btn btn-xs" %>
      <% end %>
      </div>
      <div class="col-xs-2">
      <%= number_to_currency product.price %>
      </div>
      <div class="col-xs-1">
      <%= link_to 'Delete', cart_item, method: :delete, data: { confirm: 'Are you sure?' } %>
      </div>
      <br>
      </row>
      <row>
      <hr>
    </row>
      <% end %>


  <% else %>
    <h4>"Your cart is empty"</h4>
  <% end %>

  You have <%= pluralize(@subtotal, "item")  %> in your cart


</div> 
</container>
</body>

控制器

class CartItemsController < ApplicationController
  def quantity_update
    @cart_item = CartItem.find(params[:id])
    if !cart_stuff.nil?
      if cart_stuff.save
        flash[:success] = "Quantity updated"
        redirect_to root_path
    else     
        flash[:danger] = "Update did not save"
        redirect_to root_path
      end  
    end
private

  def cart_item_params
    params.require(:cart_item).permit(:quantity, :product_id, :cart_id)
  end


end  

调试转储

  !

红宝石/哈希:ActionController的::参数

     

utf8:“✓”

     

_method:patch

     

authenticity_token:3DGIMGyRxhzNFSX / WPxeWvNHN7xFHrdAq / + HbQRsFPirfqgP3BY7ZSBIb0VwWlSdmx + ZoDO0DfrLc5IjSR0kmA ==

     

cart_item:!ruby / hash:ActionController :: Parameters     数量:'3'

     

提交:更新

     

controller:cart_items

     

行动:更新

     

id:'83'

的routes.rb

Rails.application.routes.draw do
  get 'cart_items/create'

  get 'cart_items/update'

  get 'cart_items/destroy'

  get 'categories/new'

  get 'admin_sessions/new'

  root                'static_pages#home'#'sessions#new'   #'static_pages#home'
  get 'terms'           => 'static_pages#terms'
  get 'contact'         => 'static_pages#contact'
  get 'thank_you'       => 'static_pages#thank_you'
  get 'signup'          => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'
  get 'check'           => 'sessions#new'
  post 'check'          => 'sessions#create'
  get    'adminlogin'   => 'admin_sessions#new'
  post   'adminlogin'   => 'admin_sessions#create'
  delete 'adminlogout'  => 'admin_sessions#destroy'
  get 'cart'            => 'cart_items#update'

  resources :lotnums 
  resources :microposts
  resources :admins
  resources :users
  resources :products
  resources :cart_items, only: [:create, :update, :destroy, :show, :quantity_update]
  resources :categories do
    resources :products
  end  

观看次数 - &gt;布局 - &gt; application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
    <!--[if lt IE 9]>
      <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
      </script>
    <![endif]-->
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <% flash.each do |message_type, message| %>
        <%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>  
  </body>
</html>

EDITS 添加cart_item_params私人行动代码供参考

2 个答案:

答案 0 :(得分:0)

不太确定您是否复制并粘贴,或在此处输入,但......

@cart_item = CartIem.find(params[:id])

在你的控制器中,你错过了一个&#39;来自CartItem.find(..)。

这肯定会引起一些问题。

祝你好运!

答案 1 :(得分:0)

@Eoghain你是对的,我解决了这个问题。我原始问题的最终解决方法是将'cart_item.save'替换为'cart_item.update_attributes'。一旦我做出改变,它就有效了。