Ruby on Rails + Formtastic:不选中多个复选框的复选框 [英] Ruby on Rails + Formtastic: Not checking checkboxes for multiple checked answers

查看:121
本文介绍了Ruby on Rails + Formtastic:不选中多个复选框的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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






我一直在Formtastic工作一段时间,大部分就像它如何简化表单创建。不幸的是,我碰巧使用复选框的陷阱。



代码段

  = ff.input:answer,:as => :check_boxes,:collect => 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:



params [:response] [:question_responses_attributes] =

  {
0 =>
{answer=>42,id=>1175},
...,

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

其中写入数据库为

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

我没有能够得到复选框(使用上面的代码输入),除非只有一个答案检查。



EG

  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,:collect => ff.object.survey_question.options,:checked => ff.object.answer.scan(/ \w + /),:label => ff.object.survey_question.question 

将答案分成数组。

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

这将检查所有复选框。

  = ff.input:answer,:as => :check_boxes,:collect => 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,:collect => ff.object.survey_question.options,:label => ff.object.survey_question.question 

WORKS如果只有一个选中的答案< / strong>

其他选项 b

有没有其他选择?根据Formtastic WIKI,它们不再支持:选择或:检查并提供另一个选项,设置要在初始化后模型中使用的默认值,或在控制器中使用选择和文本框。我没有找到一个工作的方式这样做与复选框。



我可以使用额外的一点js代码检查后的事实,但是我宁愿这样做,因为表单是用rails ...



先感谢您的帮助!






EDIT



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



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

  class QuestionResponse 
has_many:question_response_options
has_many:survey_options,:through => :question_response_options

#获取存储在连接
#表中的所有调查选项,并将该数组放入数组
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
#创建选项名称的哈希值为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,:collect => 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

#获取存储在连接
#表中的所有调查选项,并将该数组放入数组
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
#创建选项名称的哈希值为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,:collect => ff.object.survey_question.options,:for => :question_response_options 

输入必须是连接表,集合需要是所有选项对于给定的问题,以及:让我通过ID加入两个。



只有我必须做的是保存自己的检查选项,我做在控制器中。


EDIT: Solution at bottom and selected answer.


I've been working with Formtastic for a while and for the most part like how it simplifies form creation. Unfortunately, I've run into a snag with using checkboxes. For some reason after I save/submit my form, the checkboxes are not getting checked.

Code Snippets

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

-- .options collection method seen below:

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

Submission

The form returns the following (truncated) params:

params[:response][:question_responses_attributes] =

{
  "0"=>
     {"answer"=>"42", "id"=>"1175"},
   ...,

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

Which writes to the database as

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

I haven't been able to get the checkboxes (using the code input above) UNLESS there is only ONE answer checked. And only if the I strip out everything on submission and store the response in a custom format.

E.G.

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

will strip out the first blank option, and then split the array into a comma delimited string.

52,54,56

What I've tried so far

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

which splits the answer into an array.

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

which checks ALL of the check boxes.

= 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+/)}

which also checks ALL of the check boxes.

NOTE:

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

WORKS if there is only ONE checked answer (56) and I custom format the params before I save them to the database

Other Options??

Are there ANY other options? According to the Formtastic WIKI they no longer support :selected or :checked and offered another option setting a default value to be used in the after initialize model or in controller with a select and text box. I have not been able to find a working way to do so with checkboxes.

I'm open to using an extra bit of js code to check the boxes after the fact, but I would rather do it as the form is rendered with rails...

Thanks in advance for your help!


EDIT

I finally solved this issue. It had nothing to do with how the data was getting saved and everything to do with how I was passing the data to Formtastic.

First, I had to create join table between the question response table and the survey option table. Then I had to format how the data was accessing ALL of the survey options (based on the question) and ALL of the checked options for the question response:

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

Then in Formtastic, I had to change up how I sent information across:

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

The input had to be for the join table, the collection needed to be all of the options for the given question, and the :for let me join the two by ID.

Only thing I had to do after that is save the checked options myself, which I did in the controller.

解决方案

I finally solved this issue. It had nothing to do with how the data was getting saved and everything to do with how I was passing the data to Formtastic.

First, I had to create join table between the question response table and the survey option table. Then I had to format how the data was accessing ALL of the survey options (based on the question) and ALL of the checked options for the question response:

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

Then in Formtastic, I had to change up how I sent information across:

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

The input had to be for the join table, the collection needed to be all of the options for the given question, and the :for let me join the two by ID.

Only thing I had to do after that is save the checked options myself, which I did in the controller.

这篇关于Ruby on Rails + Formtastic:不选中多个复选框的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆