2

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.

12
  • One more thing. write else end return current_class as else current_class end Commented Mar 7, 2014 at 19:14
  • You have confused everyone more. What is A and B? Is it the value of bar? In your code you are not using @baz anywhere. All of sudden it appeared in the edited section. Commented Mar 7, 2014 at 19:17
  • 1
    @KirtiThorat OP wrote if..else..end block also wrong way. Commented Mar 7, 2014 at 19:18
  • OP - it SHOULDN'T be current_class, rather @current_class. Commented Mar 7, 2014 at 19:19
  • 1
    Neither current_class nor @current_class. It SHOULD be test_class. Commented Mar 7, 2014 at 19:21

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked. Thank you! Would up vote it but I don't have enough rep yet :|
0

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 %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.