【问题标题】:Ruby on Rails Else If QuestionRuby on Rails Else If 问题
【发布时间】:2011-01-07 20:10:33
【问题描述】:

我有一个应用程序,在布局中我有一个用户名 div,它将根据他们是否登录、是否是管理员等显示不同的内容。现在我的代码如下:

  <% if current_user.role == "admin" %>
  <p id="admintxt">You are an admin!</p>
      <%= link_to "Edit Profile", edit_user_path(:current) %>
   <%= link_to "Logout", logout_path %>
  <% elsif current_user %>
   <%= link_to "Edit Profile", edit_user_path(:current) %>
   <%= link_to "Logout", logout_path %>
  <% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>

我已经有一个 current_user 助手,并且在代码刚刚出现时一切正常:

<% if current_user %>
  <%= link_to "Edit Profile", edit_user_path(:current) %>
  <%= link_to "Logout", logout_path %>
<% else %>
    <%= link_to "Register", new_user_path %>
    <%= link_to "Login", login_path %>
<% end %>

现在,当我将其设为 elsif 语句时,当我以管理员身份登录时,它可以正常工作,并且我会使用正确的链接显示文本。当我不是管理员用户/注销时,我得到一个未定义的方法 `role' 用于 nil:NilClass 错误...此外,我的 current_user 内容在我的应用程序控制器中声明如下:

helper_method :current_user


private

def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

有什么想法可以显示我想要的结果吗? “如果他们是角色属性等于管理员的用户,他们会得到一些文本和登录链接,如果他们只是一个用户,他们会得到登录链接,如果他们没有登录,他们会得到注册和登录链接.

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:
    <% if current_user %>
      <% if current_user.role == "admin" %>
        <p id="admintxt">You are an admin!</p>
        <%= link_to "Edit Profile", edit_user_path(:current) %>
        <%= link_to "Logout", logout_path %>
      <% else %>
        <%= link_to "Edit Profile", edit_user_path(:current) %>
        <%= link_to "Logout", logout_path %>
      <% end %>
    <% else %>
      <%= link_to "Register", new_user_path %>
      <%= link_to "Login", login_path %>
    <% end %>
    

    或使用 Rails >= 2.3

    <% if current_user.try(:role) == "admin" %>
      <p id="admintxt">You are an admin!</p>
      <%= link_to "Edit Profile", edit_user_path(:current) %>
      <%= link_to "Logout", logout_path %>
    <% elsif current_user %>
      <%= link_to "Edit Profile", edit_user_path(:current) %>
      <%= link_to "Logout", logout_path %>
    <% else %>
      <%= link_to "Register", new_user_path %>
      <%= link_to "Login", login_path %>
    <% end %>
    

    【讨论】:

    • 看到这个让我真的很感激haml。 :)
    • 我一直在写erb,但直到我看到这个才意识到它有多丑。我现在很感激反应
    【解决方案2】:

    在当前用户循环中隐藏角色检查,这具有简化条件的副作用。

    <% if current_user %>
      <%= content_tag(:p, "You are an admin!", :id=>"admintxt") if current_user.role == "admin" %>
      <%= link_to "Edit Profile", edit_user_path(:current) %>
      <%= link_to "Logout", logout_path %>
    <% else %>
      <%= link_to "Register", new_user_path %>
      <%= link_to "Login", login_path %>
    <% end %>
    

    【讨论】:

    • 非常感谢,因为我正在编写循环,我知道肯定有一种方法可以简化它。我学到了很多东西!
    【解决方案3】:
    <% if current_user and current_user.role == "admin" %>
    

    当没有用户登录时,这应该可以防止错误,但是您可以重组整个块以删除针对 current_user 为 nil 的冗余测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2013-01-08
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多