我现在在商店的后台工作客户希望能够在一次表单提交中看到所有产品的列表并更新所有产品的库存值我有一个可行的解决方案,但它非常“老套”,并且引入了很多问题。一般来说,我对ruby on rails和web开发还不太熟悉,所以我仍然在学习一些基本的约定,还有其他的。
我将粘贴我的工作解决方案,然后尝试解释我遇到的问题:
class Product < ActiveRecord::Base
has_many :stocks
...
end
class Stock < ActiveRecord::Base
belongs_to :product
...
end
库存控制器.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 %>
...
当我点击submit按钮时,params集将按需要:
慰问:
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_令牌和其他参数是不允许的。现在我明白了令牌和其他参数的用途,我不知道,为什么我会遇到这个问题。
我的猜测是我允许这些情妇的方式。我不知道如何告诉强参数允许哈希数组:
stock => [{:current_stock, :product_id},{:current_stock, :product_id}, ..., ....]. params.permit(stock: [:current_stock, :product_id])
???在这种情况下,将库存嵌套在产品下是没有意义的,因为我正在处理一组产品,而不是一个产品。
在理想情况下,我希望能够在一次提交中插入所有产品的新库存值,并用一个查询保存到数据库中。我觉得ajax可能是一个可行的解决方案,但再次强调,在我完全理解所发生的事情之前,我不想让事情变得更加混乱。
任何解决方案或建议都非常感谢。我希望以上是有道理的!有时候很难把这些东西说清楚。
最佳答案
这可能是您的问题,也可能不是您的问题,但是在您的update_current方法中,应该是stock_params.permit(:current_stock, :product_id)
还有一个小问题,如果不使用表单标记,为什么要在表单标记中使用|f|
。