简体   繁体   English

如果对Devise和Rails有疑问?

[英]If questions with Devise and Rails?

I want only the people who created an object eg a registry to see something =, but struggling to define everything with Devise 我只希望那些创建对象(例如注册表)的人看到=,但很难用Devise定义所有内容

Here is the code: 这是代码:

<% if user_signed_in? && ***"current_user = registry the user created?"?(struggling with the second part)*** %>

Thanks in advance 提前致谢

PS I'm using cancan, but this will not help in my show page where I want to show page. PS我正在使用cancan,但这在我要显示页面的显示页面中无济于事。

This is not part of devise but of cancan. 这不是设计的一部分,而是康康舞的一部分。

You need to define the ability inside your cancan ability file: 您需要在cancan能力文件中定义能力:

user ||= User.new
can :read, Registry, :user_id => user.id

This way cancan's authorize method will allow you to do stuff like this: 这样,cancan的authorize方法将允许您执行以下操作:

@registry = Registry.find(params[:id])
authorize! :read, @registry

Obviously authorize! 显然是授权! will throw an error if the user has no access to this resource. 如果用户无权访问此资源,将抛出错误。

If you simply want to check this in the view the registry you use: 如果您只想在视图中检查注册表,请使用:

<% if can? :read, @registry %>
  do your thing here
<% end %>

By the way, all of this is documented on the cancan frontpage: https://github.com/ryanb/cancan 顺便说一下,所有这些都记录在cancan的首页上: https : //github.com/ryanb/cancan

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

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