【问题标题】:Ruby on Rails: OR operator in devise, not working?Ruby on Rails:设计中的 OR 运算符,不起作用?
【发布时间】:2013-09-07 19:26:11
【问题描述】:

我在设计/注册/共享/_links 中这样做是否正确?

<%- if controller_name != 'sessions' || controller_name != 'registrations' || controller_name != 'static' %>
  <%= link_to "Sign in", new_session_path(resource_name) %><br />
<% end -%>

我测试后显示登录

<%= controller_name %>

它会显示registrations 或static

谢谢

【问题讨论】:

  • 你试过答案中提到的解决方案吗?
  • @AmanGarg 感谢您的帮助!

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 devise


【解决方案1】:

如果您只想在 controller_name 不等于提到的字符串时显示链接,则不应使用 OR 运算符。

在您上面提到的示例中,如果控制器名称是注册或静态,那么它不等于“会话”。条件通过并显示链接。

检查给定字符串数组中的控制器名称不存在:

<%= link_to "Sign in", new_session_path(resource_name) unless %w(sessions registrations static).include?(controller_name) %>

或使用 AND 运算符:

<%= link_to "Sign in", new_session_path(resource_name) if controller_name != 'sessions' && controller_name != 'registrations' && controller_name != 'static' %>

它应该适合你。

【讨论】:

  • 啊,是的,这是有道理的逻辑。谢谢。
猜你喜欢
  • 2012-08-04
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多