gpt4 book ai didi

ruby-on-rails - 为什么我的 application.html.erb 中的 if 语句没有像我预期的那样工作?

转载 作者:行者123 更新时间:2023-12-04 06:27:38 25 4
gpt4 key购买 nike

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

但是,当我进入 View 时,我看到了这个:

  </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 语句似乎没有正确评估条件,因此我的布局被破坏了。

我希望看到:

这是用于招聘和表演的 ELSIF,而不是 IF 品种。

我在这里缺少什么?

最佳答案

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

action_name = "索引"

另请参阅服务器日志:

warning: found = in conditional, should be ==

关于ruby-on-rails - 为什么我的 application.html.erb 中的 if 语句没有像我预期的那样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37959789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com