0

So I am making an app to manage projects and its task, using a tutorial I have made some basic stuff.

Now I want to add a timeline to my page, so I wrote this in my pr_list.rb (model file)

def timeline
  @pr_lists.each do |pr_list|
    pr_list.pr_items.each do |pr_item|
      if pr_item.completed?
        @timeline = "#{pr_item.descrpition}: {#{pr_item.completed_at}}"
      end
    end
  end
end 

And i called it in my pr_list/views/index page.

<h1><%= pr_list.timeline %></h1>

but I get

NameError (undefined local variable or method `pr_list' for #<ActionView::Base:0x00000000029fb8>
Did you mean?  @pr_lists")

At this point even if I just code it like to output a string, it just does not work.

if I try any other things, it gives me a NoMethodError

while the rest of the methods work. and the above code also works if I write it in HTML.ERB index file as an embedded ruby.

I am new to Ruby on Rails.

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88

1 Answers1

0

pr_list is local to the block defined in the controller so it is not accessible from the template.

You could iterate on @pr_lists from within the view file (see this question for reference):

<% @pr_lists.each do |pr_list| %>
  <% pr_list.pr_item.each do |pr_item| %>
    <% if pr_item.completed? %>
      <h2><%= pr_item.description %>: <%= pr_item.completed_at %></h2>
    <% end %>
  <% end %>
<% end %>
Luc
  • 143
  • 6