【问题标题】:Ruby on Rails + Formtastic: Not checking checkboxes for multiple checked answersRuby on Rails + Formtastic:不检查多个选中答案的复选框
【发布时间】:2013-01-19 17:18:45
【问题描述】:

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


我使用 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 连接两者。

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

【问题讨论】:

    标签: ruby-on-rails checkbox formtastic


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      相关资源
      最近更新 更多