12

I have looked at loads of different articles on the web (some on here) about this issue but there are so many different suggestions, lots of which are outdated, that it has led me to ask here today...

I have a field in my Users table called admin? which is a :boolean data type. I also have a checkbox in the form in my view called admin? - I would like to be able to create TRUE and FALSE accordingly in the table record when the form is submitted.

Part of my view code is:

Admin User&#63; <%= f.check_box :admin? %>

Also I have permitted this in my post_params - is this a necessary step?

params.require(:staff).permit(:name, :email, :password, :password_confirmation, :admin?)

When I submit the form at the moment, the admin? field is unaffected. Any advice would be much appreciated.

tommyd456
  • 10,443
  • 26
  • 89
  • 163

3 Answers3

21

No need to name it ":admin?"

Just use this in your form:

Admin User&#63; <%= f.check_box :admin %>

And the permitted params like this:

params.require(:staff).permit(:name, :email, :password, :password_confirmation, :admin)

That should work.

SomeDudeSomewhere
  • 3,928
  • 1
  • 23
  • 27
  • When I use f.checkbox :admin it thows an error but not when I use admin? – tommyd456 Sep 08 '13 at 14:17
  • should the field in the table be called admin or admin? (with a question mark) - I saw somewhere it recommended using the ? in a boolean field – tommyd456 Sep 08 '13 at 14:18
  • 1
    actually I take that back. More research and I should have named it admin without the ? http://stackoverflow.com/questions/1524154/naming-boolean-columns-in-rails – tommyd456 Sep 08 '13 at 14:27
7

For anyone that has a form that isn't part of an object.

I found using checkbox syntax like:

= check_box 'do_this', '', {}, 'true', 'false'

Then in the controller side:

ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:do_this])

Will give you the correct boolean value of do_this.

Examples:

[1] pry(main)> ActiveRecord::ConnectionAdapters::Column.value_to_boolean("true")
=> true
[2] pry(main)> ActiveRecord::ConnectionAdapters::Column.value_to_boolean("false")
=> false
[3] pry(main)> ActiveRecord::ConnectionAdapters::Column.value_to_boolean("1")
=> true
[4] pry(main)> ActiveRecord::ConnectionAdapters::Column.value_to_boolean("0")
=> false

Edit: The above is deprecated, use:

ActiveRecord::Type::Boolean.new.type_cast_from_database(value)

It gives the same results as the examples above.

Rails 5: The above options don't work in Rails 5. Instead, use the following:

ActiveRecord::Type::Boolean.new.cast(value)

DickieBoy
  • 4,886
  • 1
  • 28
  • 47
  • 1
    The value_to_boolean method has been depricated, use double bang to convert any value to a boolean, see http://stackoverflow.com/questions/16848052/value-to-boolean-deprecated-whats-a-good-replacement – Alexander Popov Dec 10 '15 at 08:51
  • 1
    double bang converts 0 to true so it is not going to give you the same answer as ```value_to_boolean```. Seems like ```ActiveRecord::Type::Boolean.new.type_cast_from_database(value)``` is the current way to do it. – PressingOnAlways Jan 27 '16 at 23:41
  • @PressingOnAlways yeah the higher vote answer in the linked question is the better one. – DickieBoy Jan 28 '16 at 11:00
  • 3
    Rails 5: ActiveRecord::Type::Boolean.new.cast(string) – Darkside Dec 10 '16 at 20:46
0

It's possible you have not allowed your controller to accept the admin field through params. In other words, an issue of strong parameters. Ensure that :admin is permitted in your model_params.

rainmaker
  • 13
  • 3