B50e8f9c429811151e066e4ca85db8a4

Trying to display check boxes as checked if relationship exists between user and product

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 !

0333c8c993f63d263c9bc59ad2c35a9b

chalkers

May 24, 2008, May 24, 2008 19:07, permalink

No rating. Login to rate!

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 %>
0333c8c993f63d263c9bc59ad2c35a9b

chalkers

May 24, 2008, May 24, 2008 20:26, permalink

No rating. Login to rate!

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 %>
424a9ce662b059c35063b405e160461d

Eloy Duran

May 24, 2008, May 24, 2008 21:25, permalink

1 rating. Login to rate!

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 %>
0333c8c993f63d263c9bc59ad2c35a9b

chalkers

May 24, 2008, May 24, 2008 22:04, permalink

No rating. Login to rate!

I think you have it Eloy

Your refactoring





Format Copy from initial code

or Cancel