我目前在商店的后端工作。客户希望能够在一次提交的表单中查看所有产品的列表并更新所有产品的库存值。我有一个可行的解决方案,但它非常“hacky”并引入了很多问题。我是 Ruby on Rails 和 Web 开发的新手,所以我仍然在学习一些基本的约定,还有什么不知道的。
我将粘贴我的工作解决方案,然后尝试解释我遇到的问题:
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
...
股票指数.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
正如您从日志中看到的,authentity_token 和其他参数是不允许的。现在我了解了令牌和其他参数的目的,我不知道,为什么我会遇到这个问题。
我的猜测是我允许参数的方式。我不知道如何告诉 strong_params 允许哈希数组:stock => [{:current_stock, :product_id},{:current_stock, :product_id}, ..., ....]. params.permit(stock: [:current_stock, :product_id])
???
在这种情况下,将库存嵌套在产品下是没有意义的,因为我正在处理与单一产品相对的产品集合。
在理想情况下,我希望能够在一次提交中插入所有产品的新库存值,并通过一次查询保存到数据库中。我觉得 Ajax 可能是一个可行的解决方案,但同样,在我完全理解发生了什么之前,我不想让事情变得更加混乱。
非常感谢任何解决方案或建议。我希望以上是有道理的!有时很难把这些东西说清楚。