Topic: checkbox value always returns true value when puts to console

I have a rails app where I am using a checkbox on my view to determine which part of my if statement runs. I thought this would be an easy task but even though the response in console after clicking submit button shows both: "adult"=>"0" (when not checked) and "adult"=>"1" (when checked) but if I try
puts @application.adult
the console only prints true
Here's the relevant part of my .html.haml file...this is my most recent attempt
=simple_form_for( :application, :url => users_partner_acceptance_path( application ), :method => :put ) do | f |
  .clearfix
    .column
      =f.input :full_name, label: 'Full Name', placeholder: 'Full Name'
      .help-block * please enter your legal name
      =f.input :address_1, label: 'Address', placeholder: 'Apartment, Street'
      =f.check_box_tag :adult, inline_label: 'I am over 18 years old.', as: :boolean, checked = true
      .guardian
      =f.input :guardian_full_name, label: 'Guardian Full Name', placeholder: 'Full Name'
      =render 'partner_details_errors', application: application
  =f.submit 'Show Contract', class: 'btn btn-primary', data: { 'text' => 'Create Contract', 'disabled-text' => 'Creating ...' }
I've tried
{:checked=> true}
and
checked: true
instead of
checked = true
and probably a few others I've tried
=f.check_box :adult
and
=f.input :adult
instead of
=f.check_box_tag :adult
In my controller, I've tried to do something like this:
if @application.adult == true
  # do something
else
  # do something else
end
and
if @application.adult
  # do something
else
  # do something else
end
per someone else's suggestion, I tried:
=check_box_tag(:adult, value = "1", checked = false, options = {}) (had to add : for it to keep it from erroring but still same result as before where checkbox checked or unchecked doesn't change outcome. I then tried it with =f.check_box, :adult (error because of comma), =check_box :adult and =f.check_box :adult still no change.
Any suggestions, help is greatly appreciated. Thanks!
I'm using Rails 3.2.12

Last edited by jpgr (2013-03-18 20:36:13)

Re: checkbox value always returns true value when puts to console

1) If you want to check a boolean value use ? just checking the adult attribute without the question mark is a different check. You are checking for not nil

puts @application.adult?

2) NEVER use puts in a rails app. You have a logger object available to you with different levels

 logger.debug "application is adult #{@application.adult?}"
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

Thanks for you time. However, the purpose of my post isn't to find out how to put or log the value of adult. It's to have this value for adult change depending on whether or not a checkbox is checked or not. This is both what I want and what I need.

Re: checkbox value always returns true value when puts to console

Hello
Not sure if it can be of any help but I had something similar to do (set a cookie permanent or not based on a check-box value, if checked the cookie is permanent else it expires with the session), as you, I tried many things and I discovered the check_box_tag returns 0 or 1 as string value... So I did that, may be you can adapt to your needs ...
("remember_me" is the name of the checkbox)

if params[:session][:remember_me]!= "0"
        cookies.permanent[:remember_token] = user.remember_token
      else
        cookies[:remember_token] = user.remember_token
end

That's works for me hope it can be helpful for you

Cheers

Re: checkbox value always returns true value when puts to console

Hey anso, thanks for your reply. Could you share with me your checkbox code from your view file? Thanks!

anso wrote:

Hello
Not sure if it can be of any help but I had something similar to do (set a cookie permanent or not based on a check-box value, if checked the cookie is permanent else it expires with the session), as you, I tried many things and I discovered the check_box_tag returns 0 or 1 as string value... So I did that, may be you can adapt to your needs ...
("remember_me" is the name of the checkbox)

if params[:session][:remember_me]!= "0"
        cookies.permanent[:remember_token] = user.remember_token
      else
        cookies[:remember_token] = user.remember_token
end

That's works for me hope it can be helpful for you

Cheers

Re: checkbox value always returns true value when puts to console

Why are you using f.check_box_tag and not f.check_box ?

you stated

but if I try
puts @application.adult
the console only prints true

The reason for this is that you are using

puts @application.adult

not

puts @application.adult?

so change the form to use check_box instead of check_box_tag (you are not using a form_tag) and tag fiedls are for virtual attributes when you don't have a model object
Post your controller code and the params hash from your log file for when your form is submitted as this is where the values will be set.
How do you KNOW they are not being set when you are not using the correct if statement to determine the value in the first place?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

jamesw wrote:

Why are you using f.check_box_tag and not f.check_box ?

I have tried both and currently, I am using the code =f.check_box :adult

The reason for this is that you are using

puts @application.adult

not

puts @application.adult?

so change the form to use check_box instead of check_box_tag (you are not using a form_tag) and tag fiedls are for virtual attributes when you don't have a model object
Post your controller code and the params hash from your log file for when your form is submitted as this is where the values will be set.
How do you KNOW they are not being set when you are not using the correct if statement to determine the value in the first place?

the response in console after clicking submit button shows both: "adult"=>"0" (when not checked) and "adult"=>"1" (when checked)

I have already posted my controller code and shared what I've already tried and what you offered and anso.
for the first line of the if statement, I've tried:
if @application.adult?
if @application.adult == "1"
if @application.adult == true

that's why I've been trying to print it out to the console, to see what I should use in my if statement.

My params hash:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"removed", "application"=>{"full_name"=>"Full Name", "address_1"=>"123 Sesame Street", "adult"=>"1", "guardian_full_name"=>"G Name", "telephone"=>"3141592653", "address_2"=>"Beverly Hills, CA 90210", "guardian_email"=>"email@gmail.com"}, "id"=>"55"}

Note: the key/value pair changes to "adult"=>"0"  when checkbox isn't checked.

Last edited by jpgr (2013-03-19 06:01:04)

Re: checkbox value always returns true value when puts to console

Can you post the FULL action rather than just a snippet please. Your form is doing exactly what it should be doing

Parameters: {"utf8"=>"✓", "authenticity_token"=>"removed", "application"=>{"full_name"=>"Full Name", "address_1"=>"123 Sesame Street", "adult"=>"1", "guardian_full_name"=>"G Name", "telephone"=>"3141592653", "address_2"=>"Beverly Hills, CA 90210", "guardian_email"=>"email@gmail.com"}, "id"=>"55"}

Note: the key/value pair changes to "adult"=>"0"  when checkbox isn't checked.

so that is working perfectly. Your problem is with your action now, not with the form and it's impossible to debug the action when you haven't posted the actual code you are using

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

Started PUT "/user/partner_acceptances/55" for ip at 2013-03-19 03:20:50 -0700
Processing by Users::SomeController#update as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"removed", "application"=>{"full_name"=>"Full Name", "address_1"=>"123 Sesame Street", "adult"=>"1", "guardian_full_name"=>"G Name", "telephone"=>"3141592653", "address_2"=>"Beverly Hills, CA 90210", "guardian_email"=>"email@gmail.com"}, "id"=>"55"}

then there's a bunch of sql stuff which I cannot imagine you'd need, then

Started GET "/user/partner_acceptances55/partner_agreements/new" for ip at 2013-03-19 03:20:51 -0700
Processing by Users::SomeController#new as HTML
Parameters: {"partner_id"=>"55"}
then more sql stuff

Re: checkbox value always returns true value when puts to console

ok, that's a help
I can see that in this circumstance adult attribute is being set to a value of 1 which I assume reflects the value of the check box when you submitted the form.

"adult"=>"1",

but I still need to see the full code for the action where you are checking the value of @application.adult? as that is where your problem lies

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

I have a suspicion that you are checking the value of adult? BEFORE you have actually set the attributes on the application opbject from the params hash

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

This is what I've most recently been trying in my controller:

if @application.adult?
  # do something
else
  # do something else
end

I will grab stuff from my view now

Re: checkbox value always returns true value when puts to console

The view is fine. It's working as expected (unless you mean a different one now)

I can't really find another way of saying this but I'll ask again

Can you post the FULL code. including the def and end statements

if @application.adult?
  # do something
else
  # do something else
end

tells me absolutely nothing about what is going on. I REALLY need to see the full source code for that action
e.g.

def some_action
   @application = ....
   if @application.adult?
     whatever it is you are doing here
  else
    Whatever it is you are doing here
  render etc...
end

The FULL THING not just snippets.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

View:

=simple_form_for( :application, :url => users_partner_acceptance_path( application ), :method => :put ) do | f |
  .clearfix
    .column
      =f.input :full_name, label: 'Full Name', placeholder: 'Full Name'
      .help-block * please enter your legal name
      =f.input :address_1, label: 'Address', placeholder: 'Apartment, Street'
      =f.check_box :adult, inline_label: 'I am over 18 years old.', as: :boolean, checked: true
      .guardian
        =f.input :guardian_full_name, label: 'Guardian Full Name', placeholder: 'Full Name'
    .column
      =f.input :telephone, label: 'Telephone Number', as: :string
      .help-block  
      =f.input :address_2, label: ' ', placeholder: 'City, Country, Zip/Postcode'
      .control-group.boolean  
      .guardian
        =f.input :guardian_email, label: 'Guardian Email Address', placeholder: 'Email Address', as: :string
    .column#partner-details-errors
      =render 'partner_details_errors', application: application
  =f.submit 'Show Contract', class: 'btn btn-primary', data: { 'text' => 'Create Contract', 'disabled-text' => 'Creating ...' }


I've tried
{:checked=> true}
checked = "1"
and
checked = true
instead of
checked: true

Re: checkbox value always returns true value when puts to console

module Users
  class SomeController < ApplicationController
    include BaseConcern
    include PartnerApplicationConcern
    before_filter :set_application
   
    def new
     
      if @application.adult?

        response = request_agreement
       
        unless response.nil?
          @application.update_attribute( :agreement_key, response.document_key )
           redirect_to response.url
        end
      else

        response = request_minor_agreement
       
        unless response.nil?
          @application.update_attribute( :agreement_key, response.document_key )
        end

      end

    end
  end
end

Re: checkbox value always returns true value when puts to console

ok now we are getting somewhere

What does  before_filter :set_application do?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

from PartnerApplicationConcern controller:

module Users
  module PartnerApplicationConcern
    extend ActiveSupport::Concern
    def set_application
      @application = PartnerApplication.find params[:id] || params[:partner_acceptance_id]
      if @application.user != current_user
        youtube_session.sign_out
        flash.now[:alert] = "Please..."
        authenticate_user!
      end
    end
  end
end

Re: checkbox value always returns true value when puts to console

I can see nothing in your code that sets the params from your form to the @application object which is why the adult? attribute (or any other attributes from your form are NOT being saved other than the  :agreement_key, response.document_key

At some point BEFORE you check adult? you need to get the  params[:application] key and assign it to the @application object
Hope that makes sense

Last edited by jamesw (2013-03-19 07:08:48)

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

    def new
      @application.assign_attributes params[:application]
      if @application.adult?
        response = request_agreement
       
        unless response.nil?
          @application.update_attribute( :agreement_key, response.document_key )
           redirect_to response.url
        end
      else

        response = request_minor_agreement
       
        unless response.nil?
          @application.update_attribute( :agreement_key, response.document_key )
        end

      end

    end
  end

Should do the trick.

One other thing to check is that the attributes are included in the attr_accessible list in your model
if you don't want mass assignment on those attributes then manually assign each one
e.g.

@application.adult = params[:application][:adult]
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: checkbox value always returns true value when puts to console

Actually they are, if I try to perform an if statement with:

if @application.guardian_email == "email@gmail.com"
  # do this
else
# do that
end

it works and if I try to print @application.guardian_email to the console, the value prints...these @application.guardian_email, etc. values are used in other places within my app so trust me, they're being saved. I didn't do this view in particular, I did another one that didn't have a check box and now I'm told to use it, I'm guessing I need to perform a migration and add it as I've noticed it's not in the db.

Last edited by jpgr (2013-03-19 07:36:26)