Ruby on Rails 其他问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2111103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:10:33  来源:igfitidea点击:

Ruby on Rails Else If Question

ruby-on-railsruby

提问by Trevor Nederlof

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. 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:

现在,当我将其设为 elsif 语句时,当我以管理员身份登录时,它会起作用,并且显示的文本带有正确的链接。当我不是管理员用户/注销时,我得到一个未定义的方法 `role' for 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!

谢谢!

回答by Simone Carletti

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

回答by Eric

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

回答by Adam Crossland

<% if current_user and current_user.role == "admin" %>

This should prevent the error when there is no user logged in, but you could restructure the whole block in order to remove the redundant tests against current_user being nil.

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