简体   繁体   English

Ruby on Rails其他问题

[英]Ruby on Rails Else If Question

I have an application where in the layout I have a user_name div which will display different things based on whether or not they are logged in, are an admin, etc. Right now my code is the following: 我有一个应用程序,其中在布局中有一个user_name 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 %>

I have a current_user helper already and everything was working fine when the code was just: 我已经有一个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 %>

Now when I make it an elsif statement, when I am logged in as an admin it works and I get the text displayed with the correct links. 现在,当我将其设为elsif语句时,以管理员身份登录时它可以工作,并且显示的文本带有正确的链接。 When I am not an admin user/logged out, I get a undefined method `role' for nil:NilClass error... Also my current_user stuff is declared in my application controller as follows: 当我不是管理员用户/未登录时,我得到一个未定义的方法'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

Any ideas what I can do to display the result I want? 有什么想法可以显示我想要的结果吗? "If they are a user with the role attribute equal to admin they get some text and the logged in links, if they are just a user they get the logged in links, and if they are not logged in they get the register and login links. “如果他们是角色属性等于admin的用户,他们将获得一些文本和登录链接;如果他们只是用户,则他们将获得登录的链接;如果他们未登录,则他们获得注册和登录链接。 。

Thanks! 谢谢!

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

or with Rails >= 2.3 或使用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 %>

Hide the role check in the current user loop, which has the side effect of simplifying your conditional. 将角色检查隐藏在当前用户循环中,这具有简化条件操作的副作用。

<% 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 %>
<% if current_user and current_user.role == "admin" %>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM