我在Rails中使用Simple Form创建了一个表单来创建新的测试。我的Test
模型与has_and_belongs_to_many
模型有Tool
关联。
我的代码获取所有工具并将它们分组到类别中(category
是工具的属性)。然后,它为每个类别创建一个选择框(如图所示)。
- all_tools = Tool.all.group_by(&:category)
- all_tools.each do |category|
= f.association :tools,
collection: category.last,
label: category.first,
label_method: lambda { |tool| "#{tool.name} (#{tool.description})"},
input_html: { multiple: false }
从每个选择框中选择工具并提交表单时,我收到以下错误:
found unpermitted parameter: tool_ids
并且目前只为tool_ids
提交了一个ID,而不是我需要的一系列ID。
我知道当multiple
选择框时,这是有效的,但我只希望用户能够从每个类别中选择一个工具,并让它在数组中提交所有tool_ids,类似于用户从multiple
选择框中选择它们。
所以我的问题是,如何使用多个单一选择框以简单形式提交tool_ids
数组?
编辑: 根据要求,我已添加相关代码:
tests_controller :
def create
@test = Test.new(test_params)
@tools = Tool.all.select(:name).distinct
if @test.save
Activity.log(current_user, "Created test #{@test.id}")
redirect_to @test, notice: 'Test was successfully created.'
else
render :new, notice: 'Unable to save Test Details.'
end
end
private
# Only allow a trusted parameter "white list" through.
def test_params
params.require(:test).permit(
:date,
:start_time,
:description,
:machine,
:location,
:feeder_screw_diameter,
:notes,
:user_id,
:tool_ids => [],
:powder_ids => []
)
end
提交时记录输出:
Started POST "/tests" for 127.0.0.1 at 2016-05-08 13:24:52 +0100
Processing by TestsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"WKw+tQ1WKyxW7XZWCDvbvShCKnZ7iMY597P8eDGIvZEyis4ks7Mf4Lcu4vCf7q+fwtsReocmAlMGUrrhI4SgdQ==",
"test"=>{"start_time(1i)"=>"2016",
"start_time(2i)"=>"5",
"start_time(3i)"=>"8",
"start_time(4i)"=>"13",
"start_time(5i)"=>"23",
"description"=>"Test",
"machine"=>"Some machine",
"location"=>"Some Place",
"feeder_screw_diameter"=>"15",
"tool_ids"=>"21",
"powder_ids"=>["", "3"],
"user_id"=>"1",
"date"=>"11-05-2016",
"notes"=>"Some Notes"},
"button"=>""}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 14ms (ActiveRecord: 0.4ms)
** [Airbrake] Notice was not sent due to configuration:
Environment Monitored? false
API key set? true
ActionController::UnpermittedParameters (found unpermitted parameter: tool_ids):
app/controllers/tests_controller.rb:201:in `test_params'
app/controllers/tests_controller.rb:70:in `create'
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.0ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (12.9ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (7.9ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms)
Rendered /home/me/.rvm/gems/ruby-2.1.7/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (16.1ms)
非常感谢您的帮助!