【发布时间】:2011-12-09 08:04:28
【问题描述】:
我想为我的 User 模型添加额外的属性,但不想创建单独的 Profile 模型。 我正在尝试使用 RESTful 操作集中的标准 «update» 更新自定义字段:
class UsersController < ApplicationController
before_filter :authenticate_user!
# ...
def update
@user = User.find(params[:id])
authorize! :update, @user
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
除了 current_user 能够更新任何用户的个人资料之外,一切都很好。看来我无法限制任何用户操作。我试过了:
can :update, User, :id => user.id
和
cannot :update, User # at all
没有运气。使用 Devise 1.5.0 和 CanCan 2.0.0.alpha
这是我的能力.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new(:role => nil) # guest user (not logged in)
can :access, :all
if user.admin?
can :manage, :all
else
can :read, Review
if user.customer?
can :update, User, :id => user.id
can [:create, :update, :destroy], Review, :user_id => user.id
end
end
end
end
【问题讨论】:
标签: ruby ruby-on-rails-3 authentication devise cancan