我使用 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>
程序 Controller .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
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 %>
程序.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"
结束
最佳答案
在您的 Controller 中,您没有正确设置允许的参数。来自强参数文档:
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/