Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Checkbox in View based on Field Value

In my application I have splits and issues. I have the issue_id on my splits table and if the issue_id for a split contains 1 I want to show the checkbox for that split as checked.

I have the checkbox getting checked, but it's checking all of them regardless of what the content of issue_id is.

Here is my controller code:

@selected_split = false

if params[:issue_id] == "1"
  @selected_split = true
end

Here is an abbreviation of the code I have in my index view:

<% @splits.each do |split| %>
  <tr>
     <td><%= check_box_tag 'issue_id', 1,  {:checked => "checked"}%></td> 
     <td><%= link_to split.name, split_path(split) %> 
  </tr>
<% end %>

I've looked at this page as an example, but like I said it's checking all of the checkboxes.

All help is greatly appreciated.

like image 922
Scott S. Avatar asked Dec 28 '25 02:12

Scott S.


1 Answers

You need to pass true to 3rd parameter in check_box_tag to make the checkbox checked, something like this:

<td><%= check_box_tag 'issue_id', 1, split.issue_id == 1 %></td> 
like image 171
Alexey Shein Avatar answered Dec 30 '25 15:12

Alexey Shein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!