Ruby on Rails + Formtastic:不检查多个已检查答案的复选框

编辑:底部的解决方案和选定的答案。


我一直在与Formtastic合作一段时间,而且大部分都是如何简化表单创建。 不幸的是,我遇到了使用复选框的问题。 保存/提交表单后出于某种原因,复选框未被检查。

代码片段

= ff.input :answer, :as => :check_boxes, :collection => ff.object.survey_question.options, :label => ff.object.survey_question.question 

– .options收集方法如下所示:

 def options opts = {} survey_options.each do |option| opts[option.option] = option.id.to_s end opts end 

服从

表单返回以下(截断的)参数:

params [:response] [:question_responses_attributes] =

 { "0"=> {"answer"=>"42", "id"=>"1175"}, ..., "3"=> {"answer"=>["", "52", "54", "56"], "id"=>"1178"}, ... } 

其中写入数据库为

 --- - '' - '52' - '54' - '56' 

我无法获得复选框(使用上面的代码输入),除非只检查了一个答案。 只有当我在提交时删除所有内容并以自定义格式存储响应时。

例如

 params[:response][:question_responses_attributes].each do |key, values| if values[:answer].is_a?(Array) values[:answer] = values[:answer].delete_if {|x| x == ""}.join(",") end end 

将删除第一个空白选项,然后将数组拆分为逗号分隔的字符串。

 52,54,56 

到目前为止我尝试过的

 = ff.input :answer, :as => :check_boxes, :collection => ff.object.survey_question.options, :checked => ff.object.answer.scan(/\w+/), :label => ff.object.survey_question.question 

将答案分成数组。

 = ff.input :answer, :as => :check_boxes, :collection => ff.object.survey_question.options, :label => ff.object.survey_question.question, :input_html => {:checked => true} 

检查所有复选框。

 = ff.input :answer, :as => :check_boxes, :collection => ff.object.survey_question.options, :label => ff.object.survey_question.question, :input_html => {:checked => ff.object.answer.scan(/\w+/)} 

它还会检查所有复选框。

注意:

  = ff.input :answer, :as => :check_boxes, :collection => ff.object.survey_question.options, :label => ff.object.survey_question.question 

如果只有一个选中的答案(56),我会自动格式化参数,然后再将它们保存到数据库中

其他选择??

还有其他选择吗? 根据Formtastic WIKI,他们不再支持:选中或:选中并提供另一个选项,设置默认值以在后初始化模型中使用或在控制器中使用选择和文本框。 我无法通过复选框找到一种有效的方法。

我愿意在事后使用额外的js代码来检查框,但我宁愿这样做,因为表单是用rails渲染的…

在此先感谢您的帮助!


编辑

我终于解决了这个问题。 它与如何保存数据无关,而与我如何将数据传递给Formtastic无关。

首先,我必须在问题响应表和调查选项表之间创建连接表。 然后我必须格式化数据如何访问所有调查选项(基于问题)和问题响应的所有选中选项:

 class QuestionResponse has_many :question_response_options has_many :survey_options, :through => :question_response_options # Takes all the survey options that are stored in the join # table and puts the id's into an array def question_response_options opts = [] self.survey_options.each do |option| opts << option.id.to_s end opts end end class QuestionResponseOption belongs_to :question_response belongs_to :survey_option end class SurveyQuestion  13 } def options opts = {} survey_options.each do |option| opts[option.option] = option.id.to_s end opts end end 

然后在Formtastic中,我不得不改变我发送信息的方式:

 = ff.input :question_response_options, :as => :check_boxes, :collection => ff.object.survey_question.options, :for => :question_response_options 

输入必须是连接表,集合需要是给定问题的所有选项,并且:让我按ID加入两个。

之后我唯一需要做的就是自己保存选中的选项,这是我在控制器中做的。

我终于解决了这个问题。 它与如何保存数据无关,而与我如何将数据传递给Formtastic无关。

首先,我必须在问题响应表和调查选项表之间创建连接表。 然后我必须格式化数据如何访问所有调查选项(基于问题)和问题响应的所有选中选项:

 class QuestionResponse has_many :question_response_options has_many :survey_options, :through => :question_response_options # Takes all the survey options that are stored in the join # table and puts the id's into an array def question_response_options opts = [] self.survey_options.each do |option| opts << option.id.to_s end opts end end class QuestionResponseOption belongs_to :question_response belongs_to :survey_option end class SurveyQuestion < ActiveRecord::Base # Creates hash of option name to id # { "Law and Order" => 13 } def options opts = {} survey_options.each do |option| opts[option.option] = option.id.to_s end opts end end 

然后在Formtastic中,我不得不改变我发送信息的方式:

 = ff.input :question_response_options, :as => :check_boxes, :collection => ff.object.survey_question.options, :for => :question_response_options 

输入必须是连接表,集合需要是给定问题的所有选项,并且:让我按ID加入两个。

之后我唯一需要做的就是自己保存选中的选项,这是我在控制器中做的。