允许用户仅从索引编辑/销毁自己的配置文件 [英] Allow users to edit/destroy their own profiles only from the index

查看:117
本文介绍了允许用户仅从索引编辑/销毁自己的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Rails3,Devise和CanCan,以便在逐个用户的基础上授予编辑/销毁链接的权限。定义基于角色的能力。



我希望当前用户能够看到并访问链接,以在用户索引页面中编辑/删除他们的个人资料。他们不应该能够看到或访问所有其他用户的这些链接。



我在索引视图中设置了以下内容:

 <%如果可以的话:update,@user%> 
<%= link_to'Edit',edit_user_registration_path(@user)%> |
<%end%>

而在abilities.rb中

  def initialize(user)
can:update,User,:id => user.id
如果user.role? :super_admin
可以:管理,全部
结束
结束

我的superadmin可以查看和编辑索引中所有用户的链接。



我的用户可以看到没有人的链接,甚至没有人看到。



我读得越多,我越来越困惑,设计,cancan和用户模型都扮演一个角色。



我很感激一些确认我在正确的轨道,并将感谢任何指向有用的信息,这将有助于我了解这一点。



非常感谢

解决方案

在abilities.rb中

 可以:管理,用户做| u | 
u.id == user.id
end

如果你想



您的看法对我来说很好。


How do I grant permission to edit/ destroy links on a by-user basis in the user index of rails?

I'm using Rails3, Devise and CanCan to define role based abilities.

I'd like the current user to be able to see and access a link to edit/delete their profile in the user index page. They should not be able to see or access these links for all other users.

I've set up the following in the index view:

<% if can? :update, @user %>
     <%= link_to 'Edit', edit_user_registration_path(@user) %> | 
<% end %>

And in abilities.rb

def initialize(user) 
    can :update, User, :id => user.id
    if user.role? :super_admin
       can :manage, :all
    end
end

My superadmin can see and edit links for all users in the index.

My user can see links for no one, not even themselves.

The more I read around this the more confused I get, what with devise, cancan and the user model all playing a role.

I'd appreciate some confirmation that I'm on the right track, and would be grateful for any pointers towards useful information that would help me understand this.

Many thanks

解决方案

In abilities.rb

can :manage, User do |u|
  u.id == user.id
end

if you want to go by ID.

Your view looks good to me.

这篇关于允许用户仅从索引编辑/销毁自己的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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