简体   繁体   English

Cancan-无法保存其他角色的数据-Ruby on Rails

[英]Cancan - can't save data for other roles - Ruby on Rails

can you help my to fix my problem. 你能帮我解决我的问题吗? I can save data if the user is managing partner but when I choose other roles, (like secretary ), I can't save data to the database. 如果用户是managing partner ,我可以保存数据,但是当我选择其他角色(例如secretary )时,我无法将数据保存到数据库中。

I think, there's a problem here. 我认为这是一个问题。 This is my codes: 这是我的代码:

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

and this is my application_controller.rb 这是我的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

and this is my ability.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

In your ability.rb the 'can :update, :user_id => user.id' row is wrong. 在您的capability.rb中,'can:update,:user_id => user.id'行是错误的。 You have to specify WHAT he can update: 您必须指定他可以更新的内容:

can :update, WHAT, :user_id => user.id 可以:update,WHAT,:user_id => user.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM