【发布时间】:2014-05-24 11:26:02
【问题描述】:
你能帮我解决我的问题吗?如果用户是managing partner,我可以保存数据,但是当我选择其他角色(如secretary)时,我无法将数据保存到数据库中。
我想,这里有问题。这是我的代码:
def profile
@office = Office.last
@partial = (params[:type].present?) ? params[:type] : "work_data"
@user = User.find(params[:id])
@user.is_managing_partner = true if current_user.role == 'managing partner'
end
def update_profile
@office = Office.last
@user = User.find(params[:id])
@user.is_managing_partner = true
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
case params[:type]
when 'work_data'
redirect_to profile_user_path(type: "personal_data")
when 'personal_data'
redirect_to root_path
end
else
@partial = (params[:type].present?) ? params[:type] : "work_data"
render json: @user.errors, status: :unprocessable_entity
end
end
这是我的 application_controller.rb
rescue_from CanCan::AccessDenied do |exception|
@office = Office.last
@user = User.find(params[:id])
if @user == current_user
@partial = (params[:type].present?) ? params[:type] : "work_data"
authorize! :read, @user
render 'profile'
else
flash[:warning] = "Access Denied."
redirect_to root_url
end
end
这是我的能力.rb
if user.role == 'managing partner'
can :manage, :all
else
if user.role == "secretary"
can :update, :user_id => user.id
end
can :read, :all
end
【问题讨论】:
-
CanCan 已被放弃。改用 CanCanCan(一种替代品)。
标签: ruby-on-rails ruby gem cancan