如何使用Rails 4和simple_form正确设置复选框以保存到数据库?

时间:2014-09-12 21:30:03

标签: ruby-on-rails database checkbox simple-form

我已使用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>

programs_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 %>

program.rb

  class Program < ActiveRecord::Base
    has_many :positions
  end

schema.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"

1 个答案:

答案 0 :(得分:1)

在您的控制器中,您没有正确设置允许的参数。来自强参数docs:

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)。