Toby 1 Kenobi
Toby 1 Kenobi

Reputation: 5037

Rails form helper checkbox always checked even when {checked: false}

Ruby on Rails 4.2.1 In a view I'm making checkboxes like this:

<%= check_box_tag('roles[' + role.name + '][' + perm.name + ']', 1, {checked: role.permissions.include?(perm)}) %>

But this renders a checked checkbox every time, even when role.permissions.include?(perm) returns false

I've put debugger in the view and double checked this. inside the view when I put into the debug console this:

check_box_tag('test', 1, {checked: false})

Out comes this:

"<input type=\"checkbox\" name=\"test\" id=\"test\" value=\"1\" checked=\"checked\" />"

Is this a bug in Rails form helpers or am I missing something?

Upvotes: 2

Views: 1054

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

You should use a check_box_tag like:

check_box_tag('test', 1, false, {})

Read documentation:

 check_box_tag(name, value = "1", checked = false, options = {})

Third argument should be a boolean but you pass a hash that always return true.

Upvotes: 1

Related Questions