出于某种原因,Heroku没有通过param,这给了我一个NoMethod错误。
这是我的控制器创建动作:
def create
# @stock = Stock.new(params[:data].keys, params[:data].values)
@user = current_user
@data = params["data"]
@data.each do |d|
@stock = Stock.create(my_params(d))
if @stock.altas == nil && @stock.bajas == nil
@stock.destroy
else
@stock.save
end
@item = Item.find(@stock.item_id)
if @stock.altas != nil
@item.quantity = @item.quantity + @stock.altas
@item.save
end
if @stock.bajas != nil
@item.quantity = @item.quantity - @stock.bajas
@item.save
end
end
# ItemMailer.reporte_diario_email(@user).deliver_now
redirect_to change_path, notice: 'Cambio en el inventario creado.'
end
表单传递了params [“data”],这是我登录localhost,显示param通过了:
Processing by StocksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>
"BB/8I4Bypwa8aga8x7wCi6QWF2I9tPTuWbIitYpdpESCvOeFfPnDuaf7PjFYkjuzuz6N45t9phdbVZ1QNB3DeA==", "data"=>[{"altas"=>"", "bajas"=>"", "item_id"=>"1"}, {"altas"=>"5", "bajas"=>"", "item_id"=>"5"}, {"altas"=>"", "bajas"=>"", "item_id"=>"3"},
{"altas"=>"2", "bajas"=>"", "item_id"=>"2"}, {"altas"=>"", "bajas"=>"",
"item_id"=>"4"}], "commit"=>"Update"}
这是heroku上的日志:
Processing by StocksController#create as HTML
2016-10-25T23:23:32.440253+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"wye0t6tc1BQV15WgQsoVsnvSuwMwA8bCT77JWRqsuRNikVSq+OGhed5oIGczCmdGHl0/f+Y4/KL/YVEOOrzv/g==", "commit"=>"Update"}
最后,这是需要时的表格:
<%= form_tag stocks_path do %>
<% @items.each do |item| %>
<%= fields_for 'data[]', @stock do |stock| %>
<tr>
<td><%= item.nombre %></td>
<td><center><%= item.material %></center></td>
<td><center><%= item.marca %></center></td>
<td><center><%= item.espesor %> mm</center></td>
<td><center><%= stock.text_field :altas %></center></td>
<td><center><%= stock.text_field :bajas %></center></td>
<%= stock.hidden_field :item_id, :value => item.id %>
<% end %>
<% end %>
</tr>
</table>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<%= submit_tag "Update", class: "btn btn-primary" %>
<% end %>
正如你所看到的,它根本没有传递params [“data”],不知道为什么,因为它的代码相同......任何猜测?
在这次尝试中,几分钟后,param被加载了:
2016-10-25T23:53:24.972488+00:00 app[web.1]: Started POST "/stocks" for 65.34.251.106 at 2016-10-25 23:53:24 +0000
2016-10-25T23:53:24.980412+00:00 app[web.1]: Processing by StocksController#create as HTML
2016-10-25T23:53:24.980989+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"wye0t6tc1BQV15WgQsoVsnvSuwMwA8bCT77JWRqsuRNikVSq+OGhed5oIGczCmdGHl0/f+Y4/KL/YVEOOrzv/g==", "data"=>[{"altas"=>"", "bajas"=>"", "item_id"=>"48"}, {"altas"=>"", "bajas"=>"", "item_id"=>"49"}, {"altas"=>"", "bajas"=>"", "item_id"=>"50"}, {"altas"=>"", "bajas"=>"", "item_id"=>"51"}, {"altas"=>"", "bajas"=>"", "item_id"=>"52"}, {"altas"=>"", "bajas"=>"", "item_id"=>"53"}, {"altas"=>"", "bajas"=>"", "item_id"=>"54"}, {"altas"=>"", "bajas"=>"", "item_id"=>"55"}, {"altas"=>"", "bajas"=>"", "item_id"=>"56"}, {"altas"=>"", "bajas"=>"", "item_id"=>"57"}, {"altas"=>"", "bajas"=>"", "item_id"=>"58"}, {"altas"=>"", "bajas"=>"", "item_id"=>"59"}, {"altas"=>"", "bajas"=>"", "item_id"=>"60"}, {"altas"=>"", "bajas"=>"", "item_id"=>"61"}, {"altas"=>"", "bajas"=>"", "item_id"=>"62"}, {"altas"=>"", "bajas"=>"", "item_id"=>"63"}, {"altas"=>"", "bajas"=>"", "item_id"=>"64"}, {"altas"=>"", "bajas"=>"", "item_id"=>"65"}, {"altas"=>"", "bajas"=>"", "item_id"=>"66"}, {"altas"=>"", "bajas"=>"", "item_id"=>"67"}, {"altas"=>"", "bajas"=>"", "item_id"=>"68"}, {"altas"=>"", "bajas"=>"", "item_id"=>"69"}, {"altas"=>"", "bajas"=>"", "item_id"=>"70"}, {"altas"=>"", "bajas"=>"", "item_id"=>"71"}, {"altas"=>"", "bajas"=>"", "item_id"=>"72"}, {"altas"=>"", "bajas"=>"", "item_id"=>"73"}, {"altas"=>"", "bajas"=>"", "item_id"=>"74"}, {"altas"=>"", "bajas"=>"", "item_id"=>"75"}, {"altas"=>"", "bajas"=>"", "item_id"=>"76"}, {"altas"=>"", "bajas"=>"", "item_id"=>"77"}, {"altas"=>"", "bajas"=>"", "item_id"=>"78"}, {"altas"=>"", "bajas"=>"", "item_id"=>"79"}, {"altas"=>"", "bajas"=>"", "item_id"=>"80"}, {"altas"=>"", "bajas"=>"", "item_id"=>"81"}, {"altas"=>"", "bajas"=>"", "item_id"=>"82"}, {"altas"=>"", "bajas"=>"", "item_id"=>"83"}, {"altas"=>"", "bajas"=>"", "item_id"=>"84"}, {"altas"=>"", "bajas"=>"", "item_id"=>"85"}, {"altas"=>"", "bajas"=>"", "item_id"=>"2"}, {"altas"=>"", "bajas"=>"", "item_id"=>"6"}, {"altas"=>"", "bajas"=>"", "item_id"=>"3"}, {"altas"=>"", "bajas"=>"", "item_id"=>"4"}, {"altas"=>"", "bajas"=>"", "item_id"=>"1"}, {"altas"=>"", "bajas"=>"", "item_id"=>"86"}, {"altas"=>"", "bajas"=>"", "item_id"=>"7"}, {"altas"=>"", "bajas"=>"", "item_id"=>"8"}, {"altas"=>"", "bajas"=>"", "item_id"=>"9"}, {"altas"=>"", "bajas"=>"", "item_id"=>"10"}, {"altas"=>"", "bajas"=>"", "item_id"=>"11"}, {"altas"=>"", "bajas"=>"", "item_id"=>"12"}, {"altas"=>"", "bajas"=>"", "item_id"=>"13"}, {"altas"=>"", "bajas"=>"", "item_id"=>"14"}, {"altas"=>"", "bajas"=>"", "item_id"=>"15"}, {"altas"=>"", "bajas"=>"", "item_id"=>"16"}, {"altas"=>"", "bajas"=>"", "item_id"=>"17"}, {"altas"=>"", "bajas"=>"", "item_id"=>"18"}, {"altas"=>"", "bajas"=>"", "item_id"=>"19"}, {"altas"=>"", "bajas"=>"", "item_id"=>"20"}, {"altas"=>"", "bajas"=>"", "item_id"=>"21"}, {"altas"=>"", "bajas"=>"", "item_id"=>"22"}, {"altas"=>"", "bajas"=>"", "item_id"=>"23"}, {"altas"=>"", "bajas"=>"", "item_id"=>"24"}, {"altas"=>"", "bajas"=>"", "item_id"=>"25"}, {"altas"=>"", "bajas"=>"", "item_id"=>"26"}, {"altas"=>"", "bajas"=>"", "item_id"=>"27"}, {"altas"=>"", "bajas"=>"", "item_id"=>"28"}, {"altas"=>"", "bajas"=>"", "item_id"=>"29"}, {"altas"=>"", "bajas"=>"", "item_id"=>"30"}, {"altas"=>"", "bajas"=>"", "item_id"=>"31"}, {"altas"=>"", "bajas"=>"", "item_id"=>"32"}, {"altas"=>"", "bajas"=>"", "item_id"=>"33"}, {"altas"=>"", "bajas"=>"", "item_id"=>"34"}, {"altas"=>"", "bajas"=>"", "item_id"=>"35"}, {"altas"=>"", "bajas"=>"", "item_id"=>"36"}, {"altas"=>"", "bajas"=>"", "item_id"=>"37"}, {"altas"=>"", "bajas"=>"", "item_id"=>"38"}, {"altas"=>"", "bajas"=>"", "item_id"=>"39"}, {"altas"=>"", "bajas"=>"", "item_id"=>"40"}, {"altas"=>"", "bajas"=>"", "item_id"=>"41"}, {"altas"=>"", "bajas"=>"", "item_id"=>"42"}, {"altas"=>"", "bajas"=>"", "item_id"=>"43"}, {"altas"=>"", "bajas"=>"", "item_id"=>"44"}, {"altas"=>"", "bajas"=>"1", "item_id"=>"45"}, {"altas"=>"", "bajas"=>"", "item_id"=>"46"}, {"altas"=>"", "bajas"=>"", "item_id"=>"87"}, {"altas"=>"", "bajas"=>"", "item_id"=>"88"}, {"altas"=>"", "bajas"=>"", "item_id"=>"89"}, {"altas"=>"", "bajas"=>"", "item_id"=>"90"}, {"altas"=>"", "bajas"=>"", "item_id"=>"47"}], "commit"=>"Update"}
2016-10-25T23:53:27.334490+00:00 heroku[router]: at=info method=POST path="/stocks" host=herrajestorino.herokuapp.com request_id=f1bda6e7-6fca-4150-8dae-056de5892991 fwd="65.34.251.106" dyno=web.1 connect=1ms service=2507ms status=302 bytes=1028
这是控制器上的新操作:
def new
@stock = Stock.new
@items = Item.all
end
答案 0 :(得分:0)
这个问题已得到解决,不知道为什么,但是bootstrap sortable table会删除params一次被按下,这就是为什么它是随机行动的,我没注意到有时我按下按钮来排序它们而其他人不会,一旦我想到这个out,我删除了sortable类并在控制器中进行了命令,现在实际上看起来更好了,这就是控制器改变的方式,如果需要的话:
def new
@stock = Stock.new
@items = Item.all.order(:nombre).order(:material).order(:marca).order(:espesor)
end
修好了! 非常感谢你的帮助!!