Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6571891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:56:19+00:00 2026-05-25T14:56:19+00:00

Ive been staring at this for a good day now and i dont understand

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:56:19+00:00Added an answer on May 25, 2026 at 2:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I do have a hw question for everyone...I've been staring at this for
Good day! I know there has been lots of posts for this kind of
I've been staring at this until I can't see straight. I've got a console
I've been staring at this one for a while and I'm completely stumped. You'll
I'm sure this is something simple but I've been staring at it for a
This may be an easy answer but I've been staring at it for too
I've been at this for hours now and it's starting to get upsetting. :(
I know this is not hard, but I've been staring at the JSON.Net documentation
I've been trying to troubleshoot this, but can't find a good function that will
Maybe I've been staring at this problem for too long, maybe there isn't an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.