Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何正确设置复选框以使用Rails 4和simple_表单保存到数据库?_Ruby On Rails_Database_Checkbox_Simple Form - Fatal编程技术网

Ruby on rails 如何正确设置复选框以使用Rails 4和simple_表单保存到数据库?

Ruby on rails 如何正确设置复选框以使用Rails 4和simple_表单保存到数据库?,ruby-on-rails,database,checkbox,simple-form,Ruby On Rails,Database,Checkbox,Simple Form,我使用simple_form gem创建了一个表单,但无法保存复选框的值。单选按钮和选择字段完全保存。我已经找到了关于如何在模型中使用复选框的信息,但这不是我想要做的。我将值作为数组传递,但是当我查看html时,它看起来需要一个哈希或其他东西。如果我通过一个散列,我会得到一个讨厌的红色错误页面,告诉我我的语法不正确 我对rails相当陌生,所以可能我对这个表单的整个方法都是错误的,我应该使用一个模型?我确实为语言文件创建了一个助手,但有不同的错误,因此决定集中精力尝试保存到数据库中 关于如何正确

我使用simple_form gem创建了一个表单,但无法保存复选框的值。单选按钮和选择字段完全保存。我已经找到了关于如何在模型中使用复选框的信息,但这不是我想要做的。我将值作为数组传递,但是当我查看html时,它看起来需要一个哈希或其他东西。如果我通过一个散列,我会得到一个讨厌的红色错误页面,告诉我我的语法不正确

我对rails相当陌生,所以可能我对这个表单的整个方法都是错误的,我应该使用一个模型?我确实为语言文件创建了一个助手,但有不同的错误,因此决定集中精力尝试保存到数据库中

关于如何正确设置复选框有什么建议吗

语言html输出

<div class="control-group check_boxes optional program_languages">

<label class="check_boxes optional control-label">Languages</label>

<div class="controls">

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_english" name="program[languages][]" type="checkbox" value="English" />English</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_portuguese" name="program[languages][]" type="checkbox" value="Portuguese" />Portuguese</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_italian" name="program[languages][]" type="checkbox" value="Italian" />Italian</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_russian" name="program[languages][]" type="checkbox" value="Russian" />Russian</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_korean" name="program[languages][]" type="checkbox" value="Korean" />Korean</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_german" name="program[languages][]" type="checkbox" value="German" />German</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_vietnamese" name="program[languages][]" type="checkbox" value="Vietnamese" />Vietnamese</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_tagalog" name="program[languages][]" type="checkbox" value="Tagalog" />Tagalog</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_french" name="program[languages][]" type="checkbox" value="French" />French</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_chinese" name="program[languages][]" type="checkbox" value="Chinese" />Chinese</label>

<label class="checkbox">
<input class="check_boxes optional" id="program_languages_spanish" name="program[languages][]" type="checkbox" value="Spanish" />Spanish</label>

<input name="program[languages][]" type="hidden" value="" />

</div>
new.html.erb

<%= simple_form_for(@program, html: {class: 'form-horizontal' }) do |f| %>
  <%= f.input :programName %>
  <%= f.input :contactName %>
  <%= f.input :email, as: :email %>
  <%= f.input :phone, as: :tel %>
  <%= f.input :address %>
  <%= f.input :country, as: :country %>
  <%= f.input :state, collection: [ options_for_select(us_states)]  %>
  <%= f.input :city %>
  <%= f.input :zip %>
  <%= f.input :ageGroup, as: :check_boxes, collection: ['Any','Youth','Teen','Young Adult', 'Adult']  %>
  <%= f.input :offline, collection: ['Yes','No'],as: :radio_buttons %>
  <%= f.input :online, collection: ['Yes','No'], as: :radio_buttons %>
  <%= f.input :founded, collection: 1939..2014 %>
  <%= f.input :languages, collection: ['English','Portuguese','Italian','Russian','Korean','German','Vietnamese','Tagalog','French','Chinese','Spanish'], as: :check_boxes, include_blank: false %>
 <%= f.input :website, as: :url %>
  <%= f.input :linkedin, as: :url  %>
  <%= f.input :twitter, as: :url  %>
  <%= f.input :facebook, as: :url  %>
  <%= f.input :communityServed %>
  <%= f.input :servicesOffered %>
  <%= f.input :programsOffered %>
  <%= f.input :description %>
  <%= f.button :submit %>

结束

在控制器中,您没有正确设置允许的参数。从强参数文档:

The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

params.permit(:id => [])
那么,在你的情况下

params.require(:program).permit(..., :languages => [])
考虑到这将在数据库中保存一个ruby字符串数组的字符串表示形式

您可能需要创建一个
语言
模型,并使用一个
has\u和\u-beliens\u-to\u-many
关系(或
has\u-many:through

create_table "programs", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string   "programName"
t.string   "contactName"
t.string   "email"
t.string   "phone"
t.string   "address"
t.string   "city"
t.string   "state"
t.string   "zip"
t.string   "ageGroup"
t.string   "offline"
t.string   "online"
t.string   "founded"
t.string   "website"
t.string   "linkedin"
t.string   "twitter"
t.string   "facebook"
t.string   "communityServed"
t.string   "servicesOffered"
t.string   "programsOffered"
t.text     "description"
t.string   "languages"
t.string   "country"
The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

params.permit(:id => [])
params.require(:program).permit(..., :languages => [])