我在rails中使用simple_form,我将其作为复选框:
<%= f.input :delivery_days, as: :check_boxes,
collection: [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Satusday',
'All'],
wrapper: :vertical_radio_and_checkboxes
%>
我无法在控制器中获取所选复选框的值。
我正在使用以下代码来获取它:
def user_basic_params
logger.info "from user_basic_params"
params.require(:profile).permit(:delivery_days[])
end
我有其他表单字段,但正确处理它们并且我能够获得它们的值。
控制台输出为:
Started POST "/profiles" for 127.0.0.1 at 2015-06-02 02:07:56 +0530
Processing by ProfilesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"w3RvztJtKq56qimpZPj3KD9S5pr1vbw2K6b7eToaEV5rEuBsx1jqJ8gITg+uHq8TgBoeYZPsczQLghWg2fhpCg==", "profile"=>{"milk_animal"=>"Any", "milk_type"=>"Any", "brand"=>"Amul", "time_of_delivery(1i)"=>"2015", "time_of_delivery(2i)"=>"6", "time_of_delivery(3i)"=>"1", "time_of_delivery(4i)"=>"20", "time_of_delivery(5i)"=>"37", "start_date(1i)"=>"2015", "start_date(2i)"=>"6", "start_date(3i)"=>"1", "delivery_days"=>["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satusday", "All", ""], "name"=>"Sony", "address"=>"asdfghjk", "pincode"=>"zxcvbnm,", "contact_no"=>"2345678"}, "commit"=>"Create Profile"}
from create
from user_basic_params
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
ArgumentError (wrong number of arguments (0 for 1..2)):
app/controllers/profiles_controller.rb:53:in `[]'
app/controllers/profiles_controller.rb:53:in `user_basic_params'
app/controllers/profiles_controller.rb:18:in `create'
答案 0 :(得分:1)
问题出在代码的permit(:delivery_days[])
部分。 Rails期望括号包含1或2个参数,但是您没有传递任何参数,从而引发错误。
我不知道为什么括号在那里,尝试删除它们,我相信你的代码应该有用。
答案 1 :(得分:0)
替换此
regexp.match(string)
与
params.require(:profile).permit(:delivery_days[])