SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    978
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    looping over a span of number, needs to match day

    Alirght, as a project to learn ruby/ror I'm making a simple calendar so I'm looping from 1 - last day of month, check. Now I'm trying to check to see if there are events that rest on the same day as the current iteration day number.

    So I've got my db data stored in a @events variable from the controller, what I can't seem to figure out is the argument for the if statement inside the loop. Also, why can't I reference the column name in other methods besides 'each', everything else and the 2 entries are # signs.

    Here's the current loop
    Code:
    	<% while counter < @days_in_month + 1 -%>
    	<li <% if counter == Time.now.day -%>class="today"<% end %>>
    		<%= counter %>
    	</li>
    	<% counter += 1 -%>
    	<% end %>
    Here's what I think should work in my head
    Code:
    	<% while counter < @days_in_month + 1 -%>
    	<li <% if counter == Time.now.day -%>class="today"<% end %>>
    		<% if @events.event_date.day == counter %>
               <a href=""><%= counter %></a>
               <% else %>
               <%= counter %>
               <% end %>
    	</li>
    	<% counter += 1 -%>
    	<% end %>
    But I kinda know that won't work because it's only dealing with a single number and not looping over the @events block value.

    Any help into the logic for this mystery is appreciated.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The same way that you'd do this is another language...
    You're trying to call the "event_day" method on an array.. Where you have to call it on the individual event models..

    Code ruby:
    	<% while counter < @days_in_month + 1 -%>
    	<li <% if counter == Time.now.day -%>class="today"<% end %>>
                   <% @events.each do |event| %>
    		<% if event.event_date.day == counter %>
                        <a href=""><%= counter %></a>
                    <% else %>
                        <%= counter %>
                     <% end %>
    	   <% end %>
          </li>
    	<% counter += 1 -%>
    	<% end %>
    Does that help? Or did I misunderstand you?

    Pete
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    978
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well... event is the 'array' from the db, event_date is a date column and I thought that I could use the day method to get the day number.

    Basically what I need to do grab all events that happen in a specific month, then stick all the days from that into an array (only putting unique values). Then loop from 1 to last day of the month (making a unordered list). While it's in that loop, I need to loop over the array during each iteration to check to see if any days match the current iteration value (number). If it matches make a link, else just plain text.

    So since I pretty much explained the entire process to myself... would I grab all events and create the array in the controller and then do the loop logic and testing in the rhtml file? Would that be the correct DRY method?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    While it's somewhat off-topic and somewhat on-topic- while I know you're currently facing this problem and first thing's first (gotta tackle problems when you run into them)... you might benefit from actually taking a step back and looking at your code once you find you have some time to do so. A great deal of your logic shouldn't be in the view at all, particularly in terms of iterators, loops, and so on. It's a bit more work initially trying to wrap your head around it, but eventually you'll find that the more you split up the logic in your app, the easier it is to wrap your head around later, more complex logic. The one thing that helped me the most in this regard was a simple, easy-to-follow ten minute Railscast:

    http://railscasts.com/episodes/55

    So you might want to give it a shot later on if you have the time- should help you write cleaner, tighter code in the future.

  5. #5
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    978
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, I'll watch it. I'm coming from a coldfusion background, and ruby is turning out to be a pretty neat language as far as the basic learning curve.

    Now it's just a matter of getting this DRY stuff (what you're suggesting) down and ramming all the core methods into my head.

    Thanks for the link.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •