EricP
EricP

Reputation: 127

Passing variable within erb loop

I am new to Ruby and Rails. I am looping through a set of records and testing for age. If there is one record older than 14 days I want to set a flag that this set of records needs attention or is “bad”. I am struggling to figure out how to pass a variable into an erb loop, in this case I am using current_class.

In my controller I have this code:

def current_class
    @current_class = (params[:current_class]) 
end

In my application_helper I have the following code:

def name_class(t2 = nil, test_class)
    t1 = Time.now
    age = (t1 – t2) / 86400
    if test_class == nil || “good”
        current_class = age >= 14 ? “bad” : “good”
    else
    end
    return current_class
end

In my view I have the following code.

<% foo.each do |bar| %>
    <%= name_class bar.created_at, @current_class %>
    <%=h current_class %>
<% end %>

The output will be the evaluation of the current node but what I am trying to do is once I hit a record older than 2 weeks that current_class will have a value of “bad” for the rest of that set. Any pointers n what I am doing wrong?

Edited by request for clarity.

Generically what I am trying to do is persist a value through an erb loop.

So I have a set of 5 records named foo. Each record has an attribute named bar:

1 = A
2 = A
3 = B
4 = A
5 = A

When I loop through foo I want to persist the value of foo.bar in @baz. Once I encounter a value of B I want @baz to retain the value of B. Make sense?

Thanks in advance.

Upvotes: 2

Views: 3189

Answers (2)

Rafael Ramos Bravin
Rafael Ramos Bravin

Reputation: 660

You could use this in your view:

    <% @current_class = 'bad' if foo.any?{|bar| name_class(bar) == 'bad'} %>

    <% foo.each do |bar| %>
      <%= name_class(bar) %>
      <%h @current_class %>
    <% end %>

The first line of code will check if there's any bad records in foo and then set the @current_class to 'bad' if there is.


And in your helper method:

    def name_class(bar)
      t2 = bar.created_at
      t1 = Time.now
      age = (t1 – t2) / 86400

      return age >= 14 ? “bad” : “good”
    end

The name_class method will just check if a single bar object is bad or good

Upvotes: 3

Jay-Ar Polidario
Jay-Ar Polidario

Reputation: 6603

If I understood correctly, you want to check each foo record if foo.created_at already past 2 weeks, then you'd want to 'tag' it as Bad, otherwise if still < 14 days, 'tag' as Good. If that's so and since you want it to persist, I would suggest that you add a column/attribute to the foo's table, say a Boolean named 'is_expired'. But my code below assumes that you won't be using a new column like 'is_expired'.

Personally, I would use 'scope' inside the model file of 'foo'. But if you just really want this in the view or you're going to still do some processing with it, I'll write something like below

<%
good_foos = []
bad_foos = []
%>
<% foo.each do |f| %>
    <!-- If GOOD FOO --!>
    <% if name_class(f.created_at, @current_class) == "good" %>
        <!-- ADD FOO'S ID TO 'GOOD' ARRAY -->
        <% good_foos << f.id %>
    <!-- ELSE IF BAD FOO -->
    <% else %>
        <!-- ADD FOO'S ID TO 'BAD' ARRAY -->
        <% bad_foos << f.id %>
    <% end %>
<% end %>
<!-- PROCESS THE GOOD FOOS AND BAD FOOS HERE -->
<!--EX -->
<%= Foo.where(:id => good_foos).each do %>
    <!-- DO SOMETHING WITH FOO -->
<% end %>

Upvotes: 0

Related Questions