Cancan - 不能为其他角色保存数据 - Ruby on Rails

时间:2014-05-24 11:26:02

标签: ruby-on-rails ruby gem cancan

你可以帮助我解决我的问题吗?如果用户是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

1 个答案:

答案 0 :(得分:0)

在你的ability.rb中,可以:update,:user_id => user.id'排是错的。您必须指定他可以更新的内容:

可以:更新,WHAT,:user_id => user.id

相关问题