[英]Form_for submits data via params but the controller does not receive it - 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
調試轉儲
!ruby/hash:ActionController::Parameters
utf8:“✓”
_method:補丁
真實性_令牌:3DGIMGyRxhzNFSX/WPxeWvNHN7xFHrdAq/+HbQRsFPirfqgP3BY7ZSBIb0VwWlSdmx+ZoDO0DfrLc5IjSR0kmA==
購物車項目:!ruby/hash:ActionController::Parameters 數量:'3'
提交:更新
控制器:cart_items
行動:更新
編號:'83'
路由文件
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
視圖 -> 布局 -> 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
私有操作代碼以供參考
不太確定您是復制粘貼還是在此處輸入,但是...
@cart_item = CartIem.find(params[:id])
在您的控制器中,您缺少 CartItem.find(..) 中的“t”。
那肯定會引起一些問題。
祝你好運!
@Eoghain 你說得對,我解決了這個問題。 我的原始問題的最終解決方法是將 'cart_item.save' 替換為 'cart_item.update_attributes'。 一旦我做出改變,它就奏效了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.