Q:

Rails - If / Else in view returning both branches

I've an 'if / else' running in my view, based on the current URL of the page, and currently the view is displaying what is should were the 'if' both true and false. It's a little tricky to explain, and I've no idea why this is happening - any explanations / solutions will be greatly appreciated!
Before the code, here's a little background:

I have recipes, each of which have one or more cuisines (via has-many-through relationships)
if the URL is, for example, /italian, I want it to display all recipes with the cuisine 'Italian'
otherwise, if the URL is invalid or doesn't have any recipes with matching cuisines, I want it to display a message stating this

(So far, so straightforward right?)
However, when the code runs, it's correctly printing the right recipes (i.e. French meals won't come up on the /italian url), BUT also printing the error message. Here's the code:
In the controller:
@url = request.path.split('/')[2] #returning 'italian', 'french', etc.

And the view:
<% Recipe.all.each do |recipe| %>
  <% recipe.cuisines.each do |recipe_cuisine| %>
    <% if recipe_cuisine.name.downcase == @url %> 
      <p><strong><%= recipe.name.humanize %></strong></p>
      <ul>
        <% recipe.ingredients.each do |recipe_ingredient| %>
          <li><%= recipe_ingredient.name %></li>
        <% end %>
      </ul>
      <p><%= recipe.method %></p>
    <% else %> 
      <p>You've reached an invalid page, please return to <#%= link_to 'the homepage', root_url %></p>
    <% end %>
  <% end %>
<% end %>

To clarify, I've tested the 'recipe_cuisine.name.downcase == @url' line of code, and it's returning true when it should be, false when it shouldn't.
Does anyone know how to resolve this?
Thanks in advance, Steve.
Edit
Here are the routes that affect this:
Rails.application.routes.draw do
  get 'recipes/:cuisine' => 'recipes#cuisine'
  resources :recipes
end

A:

You defined the following route:
get 'recipes/:cuisine' => 'recipes#cuisine'

This means when you hit /recipes, it uses the cuisine action of the recipes controller (thanks to 'recipes#cuisine').
You also defined an extra :cuisine after the recipes/, which means if you hit /recipes/italian, then you will have a GET param (named cuisine) available in your controller/view.
Here is how you can use it:
# recipes_controller.rb
def cuisine
  @recipes = Recipe.all # (use `Recipe.scoped` if using Rails' version < 4)
  if params[:cuisine].present?
    @recipes = @recipes.includes(:cuisines).where(cuisines: { name: params[:cuisine] })
  end
  # other stuff
end

# cuisine.html.erb (view)
<% @recipes.each do |recipe| %>
  <p><strong><%= recipe.name.humanize %></strong></p>
  <ul>
    <% recipe.ingredients.each do |recipe_ingredient| %>
      <li><%= recipe_ingredient.name %></li>
    <% end %>
  </ul>
  <p><%= recipe.method %></p>
<% end %>

But there is a flaw in this logic: What if I hit /recipes/frenchAndMexicanPlease ? The params[:cusine] will be equal to "frenchAndMexicanPlease", and your DB does not have any cuisine type named like this. In this case, it would display no recipe at all, since the query @recipes.includes(:cuisines).where(cuisines: { name: params[:cuisine] }) would not match any existing record.

I can obviously provide more explanations about the code and logic I used. Hope this helps!

