1 2 3 4 5 6 7 8 9 10 11
<% for prod in @account.products cked = false if @user.products.nil? #if no product relationships exist, show all products as checked cked = true else if @user.products.include?(prod) cked = true end end %> <%= check_box_tag "user[product_ids][]", "#{prod.id}",checked=cked %><%= " #{prod.name}" %> <% end %>
Refactorings
No refactoring yet !
chalkers
May 24, 2008, May 24, 2008 19:07, permalink
Haven't tested it but how about this?
1 2 3 4 5
<% for prod in @account.products cked = @user.products.nil? cked = @user.products.include?(prod) if !cked %> <%= check_box_tag "user[product_ids][]", "#{prod.id}",checked=cked %><%= " #{prod.name}" %> <% end %>
chalkers
May 24, 2008, May 24, 2008 20:26, permalink
I don't think the "#{}" is nessasary either
1 2 3 4 5
<% for prod in @account.products cked = @user.products.nil? cked = @user.products.include?(prod) if !cked %> <%= check_box_tag "user[product_ids][]", prod.id, checked=cked %><%= prod.name %> <% end %>
Eloy Duran
May 24, 2008, May 24, 2008 21:25, permalink
Also not tested, but probably approximately right.
1 2 3
<% @account.products.each do |product| %> <%= check_box_tag "user[product_ids][]", prod.id, :checked => (@user.products.nil? || @user.products.include?(product)) %> <%= prod.name %> <% end %>
Trying to display check boxes as checked if relationship exists between user and product