我使用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>
程序控制器.rb
def index
@programs = Program.all
end
def new
@program= Program.new
end
def create
@program = Program.new(program_params)
if @program.save
redirect_to @program
else
render action: "new"
end
end
def edit
@program = Program.find(params[:id])
end
def update
@program = Program.find(params[:id])
if @program.update_attributes(program_params)
redirect_to programs_path
else
render action: "edit"
end
end
def show
@program = Program.find(params[:id])
end
def destroy
@program = Program.find(params[:id])
@program.destroy
redirect_to programs_url
end
def program_params
params.require(:program).permit(:programName, :contactName, :email, :phone, :address, :city, :state, :zip, :ageGroup, :offline, :online, :founded, :website, :linkedin, :twitter, :facebook, :programsOffered, :languages, :servicesOffered, :communityServed)
end
新建.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 %>
程序.rb
class Program < ActiveRecord::Base
has_many :positions
end
模式.rb
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 => [])
考虑到这将在数据库中保存ruby字符串数组的字符串表示形式。
您可能需要创建一个
Language
模型并使用一个has_and_belongs_to_many
关系(或has_many :through
)。关于ruby-on-rails - 如何使用Rails 4和simple_form正确设置复选框以保存到数据库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25817153/