Help with this Ruby Loop
Ive been staring at this for a good day now and i dont understand what ive done wrong. i want this to produce a list of activities with checkboxes, but if one of the activities is in the users_activity table then that that activities check box will be checked. However the code below displays the activity three times and chekcs all the boxes.
<fieldset>
<table>
<tr>
<th>Activity ID</th>
<th>Activity Name</th>
</tr>
<% @Activitys.each do |activity开发者_高级运维| %>
<% @users_activity.each do |ua| %>
<% if activity.id == ua.activity_id %>
<tr>
<td><%= activity.id %></td>
<td><%= activity.activity_name%></td>
<td><input name="check_<%= activity.id %>" type="checkbox" checked="yes"></td>
</tr>
<% else %>
<tr>
<td><%= activity.id %></td>
<td><%= activity.activity_name%></td>
<td><input name="check_<%= activity.id %>" type="checkbox" checked="no"></td>
</tr>
<% end %>
<% end %>
<% end %>
</table>
</fieldset>
Its probably really easy but you know what happens when you stare at a piece of code for too long...
The reason all the checkboxes are checked is that the presence of the checked
attribute on an HTML checkbox causes it to be checked regardless of the attribute's value. i.e. <input type="checkbox" checked="no">
results in a checked checkbox. For the checkboxes that should not be checked you need to write the code such that they do not have a checked
attribute at all.
The reason that you are seeing more checkboxes than you expect is because you have the @users_activity.each
loop nested inside your @Activitys.each
loop and in both the if
and the else
case you always output a checkbox so this results in @Activitys.length * @users_activity.length
checkboxes.
One solution is to collect the activity IDs of all the user activities once outside your loops i.e.
<% user_activity_ids = @users_activity.collect { |ua| ua.activity_id } %>
(you could also move this to the controller)
Then have just the @Activitys.each
loop in which you generate the checkbox with something like
<input name="check_<%= activity.id %>" type="checkbox"
<%=' checked="yes"' if user_activity_ids.include? activity.id %>>
Also, @Activitys
should probably be called @activities
as starting the variable name with a capital letter indicates that it is a constant in Ruby.
chekcs all the boxes
The reason behind all checkboxes are checked is you are using checked="no"
and checked="yes"
. There is no such thing if your write checked
in input
it will check that box.
displays the activity three times
This is happening because you have loop inside a loop. So for every activity you will print a <tr>
for every users_activity. What I'm saying is each activity is printed number of users_activity times. Create a separate logic to check if activity.id
is equal to ua.activityId
for any user and return true/false in that logic. I'm not RoR expert so I can not show for this. But this can be the logic:
for each activity do
print activity.Id
print activity.Name
if(checkInUsersActivity(activity.id))
print checked check box
else
print unchecked check box
end
function checkInUsersActivity(activityId) returns boolean
for each users_activity do
if(ua.activitId == activityId)
return true;
return false
end
Above is just outline. It is not the code in any language.
精彩评论