如果Devise和Rails有问题? [英] If questions with Devise and Rails?
问题描述
我只想要创建对象的人,例如一个注册表看到的东西=,但努力定义一切与Devise
I want only the people who created an object e.g. a registry to see something =, but struggling to define everything with Devise
这是代码:
<% if user_signed_in? && ***"current_user = registry the user created?"?(struggling with the second part)*** %>
提前感谢
我正在使用cancan,但是在我想要显示页面的展示页面中这不会有帮助。
P.S. I'm using cancan, but this will not help in my show page where I want to show page.
推荐答案
您需要定义cancan功能文件中的功能:
You need to define the ability inside your cancan ability file:
user ||= User.new
can :read, Registry, :user_id => user.id
Cancan的authorize方法将允许您执行以下操作:
This way cancan's authorize method will allow you to do stuff like this:
@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 %>
顺便提一下,所有这一切都记录在cancan首页上: https://github.com/ryanb/cancan
By the way, all of this is documented on the cancan frontpage: https://github.com/ryanb/cancan
这篇关于如果Devise和Rails有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!