2

我目前正在開店的後端。客戶希望能夠查看所有產品的列表,並在一次表單提交中更新所有產品的庫存值。我有一個工作的解決方案,但它是一個非常'哈克',並引入了很多問題。我對Ruby on Rails和Web開發一般都不熟悉,所以我仍然在學習一些基本的約定,而不是什麼。保存物品收集軌4(強參數)

我會貼上我的工作溶液中,然後試圖解釋這個問題我已經:

class Product < ActiveRecord::Base 
    has_many :stocks 
    ... 
end 

class Stock < ActiveRecord::Base 
    belongs_to :product 
    ... 
end 

stock_controller.rb

class StocksController < ApplicationController 

    def index 
    @products = Product.all.includes(:stocks) 
    end 
... 
    def update_current  
    @stock_params = params[:stock] 
    @stock_params.each do |stock_params| 
     params.permit(:current_stock, :product_id) 
     @stock = Stock.new(stock_params) 
     @stock.save 
    end 
    redirect_to stocks_path, notice: 'Stocks were successfully updated' 
end   
... 

stocks.index.html.erb

... 
<%= form_tag url_for(:action => 'update_current') do |f| %> 
    <% @products.each do |product| %> 
    <tr> 
     <td><%= product.product_name %></td> 
     <td><%= product.minimum_stock %></td> 
     <td><%= text_field_tag "stock[][current_stock]", product.stocks.last.current_stock %></td> 
    <%= hidden_field_tag "stock[][product_id]", product.stocks.last.product_id %> 
    </tr> 
    <% end %> 
    <%= submit_tag 'save' %> 
<% end %> 
... 

當我點擊提交按鈕時,參數設置爲:

控制檯:

Started POST "/stocks/update_current" for 127.0.0.1 at 2013-10-24 11:54:03 +0100 
Processing by StocksController#update_current as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NlabBuwI06t+YN5O6p7dm+Zg2Bwc9uXrKUdWaBqNs9w=", "stock"=>[{"current_stock"=>"1", "product_id"=>"1"}, {"current_stock"=>"2", "product_id"=>"2"}, {"current_stock"=>"3", "product_id"=>"24"}, {"current_stock"=>"4", "product_id"=>"25"}, {"current_stock"=>"5", "product_id"=>"23"}, {"current_stock"=>"6", "product_id"=>"21"}, {"current_stock"=>"7", "product_id"=>"19"}, {"current_stock"=>"8", "product_id"=>"22"}, {"current_stock"=>"9", "product_id"=>"5"}], "commit"=>"save"} 
Unpermitted parameters: utf8, authenticity_token, stock, commit 
(0.2ms) BEGIN 
SQL (136.6ms) INSERT INTO "stocks" ("created_at", "current_stock", "product_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Thu, 24 Oct 2013 10:54:03 UTC +00:00], ["current_stock", 1], ["product_id", 1], ["updated_at", Thu, 24 Oct 2013 10:54:03 UTC +00:00]] 
(24.2ms) COMMIT 
Unpermitted parameters: utf8, authenticity_token, stock, commit 
(0.2ms) BEGIN 
SQL (0.7ms) INSERT INTO "stocks" ("created_at", "current_stock", "product_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Thu, 24 Oct 2013 10:54:03 UTC +00:00], ["current_stock", 2], ["product_id", 2], ["updated_at", Thu, 24 Oct 2013 10:54:03 UTC +00:00]] 
(0.7ms) COMMIT 
Unpermitted parameters: utf8, authenticity_token, stock, commit 
(0.1ms) BEGIN 
SQL (0.4ms) INSERT INTO "stocks" ("created_at", "current_stock", "product_id",  "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Thu, 24 Oct 2013 10:54:03 UTC +00:00], ["current_stock", 3], ["product_id", 24], ["updated_at", Thu, 24 Oct 2013 10:54:03 UTC +00:00]] 
    (0.6ms) COMMIT 

正如你可以看到形成日誌authenticity_token,和其他PARAMS是不允許的。現在我明白了令牌和其他參數的用途,我不知道爲什麼我會遇到這個問題。

我的猜測是我允許參數的方式。我不知道如何告訴strong_params允許散列數組:stock => [{:current_stock,:product_id},{:current_stock,:product_id},...,....]。 params.permit(stock:[:current_stock,:product_id])???

在這種情況下,將產品嵌入庫存是沒有意義的,因爲我正在處理與單個產品相反的產品集合。

在一個理想的世界中,我希望能夠在一次提交中插入所有產品的新庫存值並通過一次查詢保存到數據庫。我覺得Ajax可能是一個可行的解決方案,但是,直到我完全理解正在發生的事情,我不想更加混淆事物。

任何解決方案或建議非常感謝。我希望以上是有道理的!有時候表達這些東西是非常困難的。

回答

2

這可能也可能不是你的問題,但在你的update_current方法中,不應該是stock_params.permit(:current_stock, :product_id)?另外一個小問題,如果你不使用它,你爲什麼在你的form_tag中有|f|

+0

哈哈,啊是的我想我以前在使用form_for之前就已經提到了,只是馬虎不得,我將刪除它,謝謝:))... params是ActionController :: Parameters(強參數)的一個實例,其中as我的stock_params不是這樣,如果我stock_params.permit我會得到一個NoMethodError –