基于用户在rails中的角色显示和隐藏 [英] Show and hide based on user role in rails

查看:210
本文介绍了基于用户在rails中的角色显示和隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 home.html.erb 档中有以下程式码:

 <! - 如果求助者登录在显示按钮Grab it - > 
<%if user_signed_in? %>
< div class =grabit alignright>
< small>想要解决这个问题吗?< / small>
<! - 如果没有登录,向他显示登录以处理此作业的输出文本 - >
<%else%>
< small>你是大师吗?想工作在这?注册。< / small>
<%end%>
< / div>

现在你可以看到我试图让Seeker作为用户角色。根据具有该类型角色的用户类型是否已登录,将显示不同的内容。



我安装了Devise + CanCan。现在很新的这一点,我已经看完了,看看如何做到这一点。我将有用户与正常的用户角色和寻求者与探索者角色。使用上述代码仅显示用户登录时,而不是搜索者。



它简单到 seekers_signed_in 吗?我应该使用?比较 user_signed_in ?我为用户和搜索者和管理员生成了Devise视图。管理员将能够为用户和搜索者删除,更新等。

解决方案



您不必创建用户& Seekers(两个设计模型),你可以只创建一个常见的模型,并将其称为User,然后根据需要添加任意多个角色。



a href =https://github.com/platform45/easy_roles =nofollow> gem 以方便角色配置,



然后在您的 home.html.erb 您只需执行以下操作:

  ! - 如果探索者登录显示按钮Grab it - > 
<%if user_signed_in? &&& current_user.is_seeker? %>
< div class =grabit alignright>
< small>想要解决这个问题吗?< / small>
<! - 如果没有登录,向他显示登录以处理此作业的输出文本 - >
<%else if!user_signed_in?%>
< small>你是大师吗?想工作在这?注册。< / small>
<%else%>
< small>你不是一个追求者,你需要成为一个追求者!< / small>
<%end%>
< / div>

CanCan用于Controller级别,因此上述方法对于您的情况很容易和直接。 p>

我希望这会有所帮助。


I have the following code in my home.html.erb file;

<!-- if seeker is logged in show button "Grab it" -->
<% if user_signed_in? %>
<div class="grabit alignright">
<small>want to work on this?</small>
<!-- if seeker is not logged in, show him the output text of "Sign in to work on this job" -->
<% else %>
<small>are you a guru? want to work on this? Sign up.</small>
<% end %>
</div>

Now as you can see I'm trying to have Seeker as a user role. Depending if that type of user with that type of role is signed in or not, different content is shown.

I have Devise + CanCan installed. Now very new to this and I've looked all over to see how to do this. I will have "Users" with normal roles of users and "Seekers" with seeker role. Using the above code only shows when a user is signed in, but not a seeker.

Is it as simple as seekers_signed_in? that I should be using? compare to user_signed_in? I've generated the Devise views for both Users and Seekers and Admins. Admins will be able to delete, update, etc. for users and seekers. Users post items and Seekers grab them.

Can anyone help?

解决方案

You don't have to create Users& Seekers (two devise models), instead you can create only one model as common and call it User, then add as many roles as you need.

I recommend using this gem for easy roles configuration,

Then in your home.html.erb you simply do the following:

<!-- if seeker is logged in show button "Grab it" -->
<% if user_signed_in? && current_user.is_seeker? %>
<div class="grabit alignright">
<small>want to work on this?</small>
<!-- if seeker is not logged in, show him the output text of "Sign in to work on this job" -->
<% else if !user_signed_in?%>
<small>are you a guru? want to work on this? Sign up.</small>
<% else%>
<small>You are not a seeker, you need to be a seeker!</small>
<% end %>
</div>

CanCan is used at Controller level, so the above approach is easy and direct for your case.

I hope this will help.

这篇关于基于用户在rails中的角色显示和隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆