【发布时间】:2019-12-13 21:30:46
【问题描述】:
我正在使用管理框架“活动管理员”来构建 RubyOnRails 应用程序。我的表单中有一个如下所示的下拉菜单:
form do |f|
f.inputs do
f.input :art, as: :select, :include_blank => "Bitte wählen!", label: 'art', :collection => ["Fachobjekt", "Gruppe", "Externes Dokument"]
end
f.actions
end
现在在我的控制器中,我想检查选定的值。我试过了:
controller do
after_save :update_object
def update_object(guid)
if params[:art].values == 'Fachobjekt'
# do stuff
end
end
end
我在下拉列表中选择了“Fachobjekt”,但我得到了NoMethodeError "undefined method 'values' for nil:NilClass",因此params[:art] 为空。
我的问题是:获取 f.input-field 的选定值的正确语法是什么?我很感激任何提示!
[:root_tables] 是模型名称。我把它放在 [:art] 之前,就像 params[:root_tables][:art] 但同样的错误。
【问题讨论】:
-
型号是什么?
-
[:root_tables] 是模型名称。我把它放在 [:art] 之前,就像 params[:root_tables][:art] 但同样的错误。
-
嗯。它应该返回正确的参数。你使用允许的参数吗?因此,如果您清理参数,您将能够以 allowed_params[:root_tables] 的形式访问它们
-
所以,也许可以使用
binding.pry或类似方法进行一些基本调试。把它放在方法的最上面,看看你收到了什么参数。或者甚至对它们进行简单的打印(实际上 rails 在每个请求上都打印参数)发布这些参数,然后可能更容易找到原因