Reputation:
I have a check box and I want the checkbox to be checked if a parameter equals 1. So I need the :checked to only be true if the condition is true.
<%= check_box_tag 'catalog_image', 1, :onclick => 'this.form.submit()', :checked => (params[:catalog_image] == 1)%>
The current condition I am testing does not work. The param will automatically be added, or subtracted, when the form submits because it is linked back to the same page.
Upvotes: 4
Views: 10543
Reputation: 326
The API say: Rails check_box_tag
check_box_tag(name, value = "1", checked = false, options = {}) public
So you can improve this in one single line:
<%= check_box_tag 'catalog_image', 1, <CONDITION>, :onclick => 'this.form.submit()' %>
Upvotes: 5
Reputation: 13925
Use if to this, because the presence of the :checked
tag that matters, not it's value
<% if params[:catalog_image] == 1 %>
<%= check_box_tag 'catalog_image', 1, :onclick => 'this.form.submit()', :checked =>true %>
<% else %>
<%= check_box_tag 'catalog_image', 1, :onclick => 'this.form.submit()'%>
<% end %>
Upvotes: 4