This is a call for help relating to Ruby on Rails to stop my blood pressure getting to dangerous levels...
Ruby Version: ruby 2.1.0p0 (2013-12-25 revision 44422)
Rails Version: Rails 4.0.3
Essentially I'm creating a web app that lets you get feedback on a design, but I am having trouble with the 'unless' statement when it comes to displaying headings on tests#index page
Controller (this works fine for retrieving the right tests):
def index
# Gets all tests that have an end date before now
@completed_tests = Test.where(["end_date < ?", DateTime.now]).order("end_date asc")
# Gets all tests that have an end date after now
@current_tests = Test.where(["end_date > ?", DateTime.now]).order("end_date asc")
end
View (tests#index) (this is the section that is causing me problems)
# This should be if there are no tests for the user, display the Blank state text
<% unless @completed_tests.nil? || @current_tests.nil? %>
# If there are no completed tests this should not be displayed
<% unless @completed_tests.nil? %>
<h3>Completed Tests Section Header</h3>
<% end %>
<% @completed_tests.each do |test| %>
# Do stuff (this works fine)
<% end %>
# If there are no current tests this should not be displayed
<% unless @current_tests.nil? %>
<h3>Current Tests Section Header</h3>
<% end %>
<% @current_tests.each do |test| %>
# Do stuff (this works fine)
<%end%>
<% else %>
# This should be displayed if there are no completed or current tests
<p>Blank state text</p>
<% end %>
The problem in that the headers are showing when there is no tests, e.g. the completed tests header is showing when there are no completed tests and it shouldn't be, equally if there are no tests I'm getting the completed and current test headers instead of seeing the blank state text.
Other things I've tried:
<% if @current_tests.nil? %>
<h3>Completed Tests Section Header</h3>
<% end %>
Above: This gets rid of the header regardless of whether there are tests or not
<% if @current_tests %>
<h3>Completed Tests Section Header</h3>
<% end %>
Above: Shows it regardless of whether there are tests or not (same as original code above)
Any help would be greatly appreciated...