0

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


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

提交

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

参数[: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,他们不再支持 :selected 或 :checked 并提供另一个选项设置默认值,用于在初始化后的模型或带有选择和文本框的控制器中使用。我无法找到使用复选框的工作方式。

我愿意在事后使用额外的 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 < 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

输入必须是连接表,集合必须是给定问题的所有选项,并且 :for 让我通过 ID 连接两者。

在那之后我唯一要做的就是自己保存检查的选项,我在控制器中做了。

4

1 回答 1

3

我终于解决了这个问题。它与如何保存数据无关,也与我如何将数据传递给 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

输入必须是连接表,集合必须是给定问题的所有选项,并且 :for 让我通过 ID 连接两者。

在那之后我唯一要做的就是自己保存检查的选项,我在控制器中做了。

于 2013-02-06T14:50:07.027 回答