我目前正在开设商店的后端。客户希望能够在一个表单提交中查看所有产品的列表并更新所有产品的库存值。我有一个有效的解决方案,但这是一个非常'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
...
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 %>
...
当我点击提交按钮时,设置就像它需要的那样:
console:
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,其他参数不被允许。现在我理解令牌和其他参数的目的,我不知道,为什么我遇到了这个问题。
我的猜测是我允许参数的方式。我不知道如何告诉strong_params允许一系列哈希:stock => [{:current_stock, :product_id},{:current_stock, :product_id}, ..., ....]. params.permit(stock: [:current_stock, :product_id])
???
在这种情况下,将库存嵌入产品中是没有意义的,因为我正在使用一系列与单一产品相对的产品。
在理想的世界中,我希望能够在一个提交中插入所有产品的新库存值,并使用一个查询保存到数据库中。我觉得Ajax可能是一个可行的解决方案,但同样,在我完全理解发生了什么之前,我不想让事情更加混乱。
非常感谢任何解决方案或建议。我希望以上是有道理的!有时候很难清楚地表达这些事情。
答案 0 :(得分:2)
这可能是您的问题,也可能不是,但在您的update_current方法中,它不应该是stock_params.permit(:current_stock, :product_id)
吗?另外还有一个小问题,如果你不使用它,为什么你的form_tag中有|f|
。