【问题标题】:Why does this if statement in my application.html.erb not work like I expect?为什么我的 application.html.erb 中的 if 语句不像我预期的那样工作?
【发布时间】:2016-10-23 21:48:52
【问题描述】:

在我的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" = $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)

但是,当我去视图时,我看到了这个:

  </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 变体。

我在这里错过了什么?

【问题讨论】:

  • 日志中有警告可能是原因。 warning: found = in conditional, should be == 。在您的第一个条件中,您设置了action_name = "index"
  • @IngoAlbers 伙计!!!我完全错过了。非常感谢。就是这样。你能添加那个答案吗?我会接受的。

标签: ruby-on-rails erb actionview ruby-on-rails-5


【解决方案1】:

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

action_name = "index"

另见服务器日志:

警告:在条件中找到 =,应该是 ==

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      <% 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" 中从赋值到相等的变化

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-07
        • 2014-10-14
        • 1970-01-01
        • 2019-04-19
        • 2012-06-13
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多