Writing an "upto" block in Rails

I am trying to loop through records in a db and pull out a particular
field. I have no problem with the code when I run it in the console;
however when I try to transfer the code to my Rails environment it
fails.

The code I’m using in my console is:

show = Schedule.find(:all)
n = show.size - 1
0.upto(n) { |i| puts show[i].venue }

The above code runs perfectly. To adapt this code for Rails, I am doing
the following:

(IN THE CONTROLLER)
class ScheduleEditController < ApplicationController
def index
@show = Schedule.find(:all, :order => ‘date ASC’)
end
end

(IN THE VIEW)

    <%= render :partial => 'shows' %>

(IN THE PARTIAL)
<% n = @show.size - 1 %>

  • <%= 0.upto(n) { |i| puts @show[i].date } %>
  • The result I get is this:

    • 0

    The result I am looking for is:

    • 2
    • 3
    • 1

    Yes, I am obviously a noob. Please help shed light on where I’ve gone
    wrong before I tear all my hair out.

    On 27 Mar 2008, at 19:28, Sara Me wrote:

    (IN THE PARTIAL)
    <% n = @show.size - 1 %>

  • <%= 0.upto(n) { |i| puts @show[i].date } %>
  • When you have a n erb output block (<%= … %>) what appears in the
    view is what the expression evaluates to. calls to puts inside there
    are immaterial n.upto evaluates to n, hence the result you’re getting
    You need something like

    <% 0.upto(n) do %>

  • <%= @show[i].date %>
  • <% end %>

    That’s not a very idiomatic way of doing it though. More idiomatic
    would be
    <% @show.each do |show| %>

  • <%= show.date %>
  • <% end %>

    or using a partial (although it might be a bit overkill in this
    particular case)

    Fred

    Sara Me wrote:

    (IN THE PARTIAL)
    <% n = @show.size - 1 %>

  • <%= 0.upto(n) { |i| puts @show[i].date } %>
  • The #puts method won’t output into the view…

    In the view, put:

    <%= render :partial => ‘shows’, :collection => @show %>

    This will cause the partial to run for each item in the @show list.
    This saves you from iterating over them yourself.

    Then, in the partial, put:

  • <% shows.date %>
  • There will be a local variable of the same name as the partial (shows in
    this case) for each item in the collection.

    For your iteration in your console session, you could more easily do:

    show = Schedule.find(:all)
    show.each {|s| puts s.venue}

    It is very rare in Ruby code to ever need to refer to indices at all.

    Jon G. wrote:

    First change your @show to @shows (it has more than one, so for
    readability, make it plural)
    Second, change your partial to _show, not _shows.

    Next
    In your view, do this…

      <%= render :partial => 'show', :collection => @shows %>

    And then your partial can just be…

  • <%= show.date %>
  • No need to to the upto loop. passing :collection to render will iterate
    through items and re-render the partial for each.

    YESSSS!!! Thank you so much. I understand it now.

    Sara Me wrote:

    (IN THE VIEW)

      Yes, I am obviously a noob. Please help shed light on where I've gone wrong before I tear all my hair out.

    First change your @show to @shows (it has more than one, so for
    readability, make it plural)
    Second, change your partial to _show, not _shows.

    Next
    In your view, do this…

      <%= render :partial => 'show', :collection => @shows %>

    And then your partial can just be…

  • <%= show.date %>
  • No need to to the upto loop. passing :collection to render will iterate
    through items and re-render the partial for each.

    http://www.5valleys.com/

    http://www.workingwithrails.com/person/8078

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs