Scott S.
Scott S.

Reputation: 759

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.

Upvotes: 3

Views: 754

Answers (1)

Aleksey Shein
Aleksey Shein

Reputation: 7482

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> 

Upvotes: 1

Related Questions