2

In my application.html.erb, I have the following:

      <% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name = "index") %>
        <%= "This is the IF for #{controller_name} and #{controller.action_name}" %>
            <%= yield %>
      <% elsif current_page?(controller: "jobs", action: "show") %>
        <%= "This is the ELSIF for #{controller_name} and #{controller.action_name}" %>
          <div class="wrapper wrapper-content article">
            <%= yield %>
          </div>
      <% else %>
        <%= "This is the ELSE for #{controller_name} and #{controller.action_name}" %>
       <% end %>

Then I navigated to a Jobs#Show page, see the logs here:

Started GET "/jobs/social-media-manager" for 127.0.0.1 at 2016-06-22 01:00:05 -0500
Processing by JobsController#show as HTML
  Parameters: {"id"=>"social-media-manager"}
  Job Load (1.5ms)  SELECT  "jobs".* FROM "jobs" WHERE "jobs"."slug" = $1 ORDER BY "jobs"."id" ASC LIMIT $2  [["slug", "social-media-manager"], ["LIMIT", 1]]
  Rendering jobs/show.html.erb within layouts/application
  User Load (1.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1546], ["LIMIT", 1]]
  Role Load (1.7ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1546]]
  Rendered jobs/show.html.erb within layouts/application (18.8ms)
/app/views/layouts/application.html.erb:21: warning: found = in conditional, should be ==
  Rendered shared/_navbar.html.erb (2.6ms)
  Rendered shared/_footer.html.erb (0.8ms)
Completed 200 OK in 228ms (Views: 218.1ms | ActiveRecord: 4.8ms)

However, when I go to the view, I see this:

  </nav>
</div>

            This is the IF for jobs and show
                <div class="row">
    <div class="col-lg-10 col-lg-offset-1">

So even though my server logs are telling me that I sent a request to the Jobs#Show, the IF statement seems to be not evaluating the conditions correctly, and so my layout is broken.

I would have expected to see:

This is the ELSIF for jobs and show, not the IF variety.

What am I missing here?

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • There is a warning in the logs that might be the reason. `warning: found = in conditional, should be ==` . In your first condition you set `action_name = "index"`. – IngoAlbers Jun 22 '16 at 06:18
  • @IngoAlbers DUDE!!! I completely missed that. THANKS MUCH. That was it. Can you add that answer and I will accept it. – marcamillion Jun 22 '16 at 06:20

3 Answers3

2

You missed one = in second condition

<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name = "index") %>

Change it to

<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name == "index") %>
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
2

This is because you don't check for the action_name in your first condition, but instead set it.

action_name = "index"

See also the server logs:

warning: found = in conditional, should be ==

IngoAlbers
  • 5,722
  • 6
  • 31
  • 45
1
<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name = "index") %>

Should be

<% if (controller_name == "questions" && action_name == "index") || (controller_name == "jobs" && action_name == "index") %>

Note the change from assignment to equality in action_name = "index" to action_name == "index"

jamesc
  • 12,423
  • 15
  • 74
  • 113