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.
else end return current_classaselse current_class endAandB? Is it the value ofbar? In your code you are not using@bazanywhere. All of sudden it appeared in the edited section.if..else..endblock also wrong way.current_class, rather@current_class.test_class.