Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:50:23+00:00 2026-06-18T09:50:23+00:00

EDIT: Solution at bottom and selected answer. I’ve been working with Formtastic for a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:50:24+00:00Added an answer on June 18, 2026 at 9:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Intro: EDIT: See solution at the bottom of this question (c++) I have a
EDIT: Solution in code can be found on bottom. I am not sure if
Edit at bottom with solution I've seen a similar question to this posted before
EDIT : see bottom First off I searched for an answer before asking this
EDIT The solution has been found! Here's a blog post about it, and here
I have been trying to edit this easy form to just look good for
I've been searching for quite a while for a solution about this but no
EDIT: Sorted. See bottom for solution Okay, I have to write a program that
EDIT: Solution at bottom of post. I've got a custom GreaseMonkey script I made
EDIT : proper solution: void add(Student s) { if(root == null) root = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.