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

  • Home
  • SEARCH
  • 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 5970527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:23:15+00:00 2026-05-22T20:23:15+00:00

Have a boolean field (using formtastic) and the value won’t persist through an error.

  • 0

Have a boolean field (using formtastic) and the value won’t persist through an error. If submitted and an error exists, on reload, the boolean field automatically checks itself, which is bad. I can see the problem is in the conflicting value, hidden is 0 whereas the input is 1. Both should be 0. How do I correct this so both are set to 0 initially. Where am I going wrong?

Form Code:

<%= f.input :legal, :as => :boolean %>

Html Generated

<li class="boolean optional" id="user_legal_input">
   <input name="user[legal]" type="hidden" value="0" />
   <label for="user_legal">
      <input id="user_legal" name="user[legal]" type="checkbox" value="1" />I Agree to the legal terms
   </label>
</li>

ADDED: User (Create) Controller

def create
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        format.js  
      else
        format.js  
      end  
    end
  end

The js just re-renders the form with all the errors inline

  • 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-05-22T20:23:16+00:00Added an answer on May 22, 2026 at 8:23 pm

    the hidden field is always set to “0” to mimic an unchecked box, this is correct behaviour:
    http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box

    Instead I suggest your isssue is your accessor method in the model.

    The checkbox input is checked if @user.legal is true type, or more precisely if

    ActionView::Helpers::InstanceTag.check_box_checked?(@user.legal,"1")
    

    evaluates to true.

    Now if legal is a boolean database column, rails initializer will convert it to ruby boolean so if you do

    @user = User.new(:legal => "0")
    

    then

    @user.legal == false
    

    but if legal is not a db column (otherwise :as => :boolean is unnecessary), then you must have defined an accessor method somehow.

    If you did it with a simple attr_accessor then,

    @user.legal == "0"
    

    which correctly evaluates to an unchecked box.

    but if you or your framework wanted to outsmart rails and you define:

    attr_writer :legal
    def legal
     !!@legal
    end
    

    or something similar under the hood (to give you a proper boolean back), then you are in trouble:

    Here initial form display !!nil is false => unchecked box.
    but on submit/reload !!"0" is true => checked box.

    let me know if my hunch is correct 🙂

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

Sidebar

Related Questions

I want to stop threads by using a boolean field. I have implemented some
I have a class that contains a boolean field like this one: public class
I want to set the value of a private field using reflection for unit
I have an Oracle database and the boolean fields are CHAR(1) with Y/N stored.
I have a Struts (1.3x) ActionForm that has several String and boolean properties/fields, but
Why isn't Collection.remove(Object o) generic? Seems like Collection<E> could have boolean remove(E o); Then,
I have a boolean variable declared at the top of a class and when
The case goes as following: You have a Boolean property called FullScreenEnabled. You enter
Since MySQL doesn't seem to have any 'boolean' data type, which data type do
I have a class isSearching with a single boolean property in a 'functions' file

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.