为什么我的 application.html.erb 中的这个 if 语句不能像我预期的那样工作?

Why does this if statement in my application.html.erb not work like I expect?

在我的 application.html.erb 中,我有以下内容:

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

然后我导航到 Jobs#Show 页面,在此处查看日志:

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" =  ORDER BY "jobs"."id" ASC LIMIT   [["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" =  ORDER BY "users"."id" ASC LIMIT   [["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" =  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)

然而,当我进入视图时,我看到了这个:

  </nav>
</div>

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

所以即使我的服务器日志告诉我我向 Jobs#Show 发送了一个请求,IF 语句似乎没有正确评估条件,所以我的布局被破坏了。

我希望看到:

This is the ELSIF for jobs and show,不是 IF 变体。

我在这里错过了什么?

你在第二种情况下错过了一个=

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

改为

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

应该是

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

请注意 action_name = "index"action_name == "index"

中从赋值到相等的变化

这是因为您在第一个条件中没有检查 action_name,而是设置了它。

action_name = "index"

另请参阅服务器日志:

warning: found = in conditional, should be ==