2016-10-25 14 views
3

私は実際にコードをレールに書くための基本的なことを理解するのに苦労しています。私はそれがより基本的な質問をすることであるか分からないが、私は同じような問題を繰り返しているようだ。Rails 5、Rolify - ユーザーからロールを外す

私は自分のRails 5アプリケーションでrolifyをセットアップすることができました。私はrolifyを使ってユーザーに役割を割り当てます。

ここでは、割り当てられた後にユーザーからロールを削除する機能をセットアップしようとしています。

私のユーザーインデックスでは、

<% user.roles.each do |role| %> 
    <%= role.name.titleize %> <br> 
<% end %> 

これは、割り当てられた役割を示しています。

その後、イムは、追加しようとしている:

<%= link_to 'Destroy', role, method: :delete, data: { confirm: 'Are you sure?' } %> 

は方法が私のassign_rolesコントローラで定義されて破壊するマイ。それはあります

def destroy 

    user = User.find(params[:users]) 
    # role = AppRole.find(params[:roles]) 
    assigned_role = user.roles 
    # user_roles = user.roles 
    # organisation = Organisation.first 
    # byebug 

    user.remove_role assigned_role.name, @current_user.organisation 

    flash[:notice] = "Successfully removed" 
    redirect_to action: :index 
    end 

ルートは以下のとおりです。

resources :users, shallow: true do 
    scope module: :users do 
     resources :assign_roles 
    end 

assign_role.rbモデルはありません。私はちょうどコントローラとビューを使用します。

私は私のユーザーインデックスに破壊する役割のリンクを使用しようとすると、私が言うエラーが出る:

Couldn't find User with 'id'= 

を誰もが、私は#が動作するようにアクションを破壊assign_rolesを取得するために何をする必要があるか確認することはできますか?

assign_rolesコントローラの作成アクションが動作します。それはあります

def create 
    user = User.find(params[:users]) 
    role = AppRole.find(params[:roles]) 
    # organisation = Organisation.first 
    @organisation = Organisation.find(@current_user.organisation) 
    # byebug 

    user.add_role role.display_name, organisation 

    flash[:notice] = "Successfully created" 
    redirect_to action: :index 
    end 

全体の割り当て役割コントローラ:

class Users::AssignRolesController < ApplicationController 

    before_action :authenticate_user! 

    def index 
    # if @current_user.is_admin? 
     @app_roles = AppRole.all 
    # else 
    # @app_roles = AppRole.where(category: relevant_category) 
    # end 

    # if @current_user.is_admin? 
     @users = User.all 
    # else 
     # @users = current_user.organisation.users.select { |u| u.id != current_user.organisation.owner_id } 
    # end 
    end 


    def create 
    user = User.find(params[:users]) 
    role = AppRole.find(params[:roles]) 
    # organisation = Organisation.first 
    @organisation = Organisation.find(@current_user.organisation) 
    # byebug 

    user.add_role role.display_name, organisation 

    flash[:notice] = "Successfully created" 
    redirect_to action: :index 
    end 

    def show 
    # @users = User.joins(:profiles).where('profiles.organisation_id = ?' @current_user.organisation.id) 
    # @users = User.all 
    @current_user.organisation.users 
    end 

    def update 
    end 

    def destroy 

    user = User.find(params[:users]) 
    # role = AppRole.find(params[:roles]) 
    assigned_role = user.roles 
    # user_roles = user.roles 
    # organisation = Organisation.first 
    # byebug 

    user.remove_role assigned_role.name, @current_user.organisation 

    flash[:notice] = "Successfully created" 
    redirect_to action: :index 
    end 

end 

私はコンソールからやりたいことができます - 私は、コードからそれを行う方法を見つけ出すCANT

コンソールで

、私は書くことができます:

u.remove_role :fff, Organisation.first 

これでロールが正常に削除されます。

これはテストとしては問題ありませんが、コードではOrganisation.firstの代わりにcurrent_user.organisationを使用しようとしています。私の目的は、ユーザーが自分の組織の役割だけを管理できるようにすることです。

私のassign rolesコントローラでは、上にコピーされたdestroyアクションがあります。私のユーザーのインデックスで

として、私は、ユーザーから割り当てられた役割を削除しようとしています。エラーが言う

<% user.roles.each do |role| %> 
       <table class="table table-bordered"> 
       <tr> 
       <td><%= role.name.titleize %></td> 
       <td><%= link_to 'Remove role', role, method: :delete, data: { confirm: 'Are you sure?' } %></td> 

undefined method `role_path' for #<# 

私はそれに何かを与える必要がある場合、私は疑問に思いますそれ以外のパスにはassign roles#destroyアクションがあります。

私はルートをぶつけるとき| grepのアサインは、私が見ることができます:

DELETE /assign_roles/:id(.:format)            users/assign_roles#destroy 

私はに私のユーザーのインデックスでパスを変更してみました:

<% user.roles.each do |role| %> 
       <table class="table table-bordered"> 
       <tr> 
       <td><%= role.name.titleize %></td> 
       <td><%= link_to 'Remove role', assign_role_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
       </tr> 
       </table> 
      <% end %> 

しかし、その後、このエラーを与えること:

Couldn't find User with 'id'= 

私にはありませんこのエラーの意味を理解してください。

もう一つの手掛かり

右なので、問題が発生した場所を私は見ることができます。私はそれを修正する方法のアイデアがありません。

コンソールで

、私は正常にこのことで私が好きな方法でユーザーの役割を削除することができます

:コードで

u.remove_role :sdfddd, Organisation.first 
    Organisation Load (2.0ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1 [["LIMIT", 1]] 
    Role Load (0.7ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND "roles"."name" = $2 AND "roles"."resource_type" = $3 AND "roles"."resource_id" = $4 [["user_id", 4], ["name", "sdfddd"], ["resource_type", "Organisation"], ["resource_id", 1]] 
    (0.1ms) BEGIN 
    SQL (0.4ms) DELETE FROM "users_roles" WHERE "users_roles"."user_id" = $1 AND "users_roles"."role_id" = 6 [["user_id", 4]] 
    (1.6ms) COMMIT 

を、私の現在のは言う割り当て役割コントローラからアクションを破壊します

def destroy 

    # user = User.find(params[:users]) 
    user = User.find(params[:id]) 
    # role = AppRole.find(params[:roles]) 
    assigned_role = user.roles 
    # user_roles = user.roles 
    organisation = Organisation.first 
    # organisation = Organisation.find(current_user.organisation) 

    # byebug 

    user.remove_role assigned_role.name, organisation 

    flash[:notice] = "Successfully created" 
    redirect_to root_path 
    end 

ログにこのプロセスを示している:

User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]] 
    User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]] 
    Organisation Load (0.7ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1 [["LIMIT", 1]] 
    Role Load (1.0ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND "roles"."name" = $2 AND "roles"."resource_type" = $3 AND "roles"."resource_id" = $4 [["user_id", 4], ["name", "Role"], ["resource_type", "Organisation"], ["resource_id", 1]] 

2番目のパラメータが「Roのありますそれはおそらく役割の名前でなければなりません(私は思う)。ロールはテーブル名です。

私はこの作業を行うためにこれをどのように接続するのかは分かりません。誰もが私はコンソールでそれを行うことができる方法で処理するコードを取得することができますか?

次の試行

はassign_rolesコントローラでの破壊行為で私の新しい試みがあります

def destroy 

    # user = User.find(params[:users]) 
    user = User.find(params[:id]) 
    # role = AppRole.find(params[:roles]) 
    assigned_role = user.roles 
    # user_roles = user.roles 
    # organisation = Organisation.first 
    organisation = Organisation.find(current_user.organisation) 

    # byebug 

    user.remove_role assigned_role, organisation 

    flash[:notice] = "Successfully created" 
    redirect_to root_path 
    end 

私は問題のある行はこの1つだと思う:

assigned_role = user.roles 

それが必要私が(割り当てられた役割のユーザーの配列から)破壊しようとしている特定の役割

ログが示しています

[["user_id", 4], ["name", "#<Role::ActiveRecord_Associations_CollectionProxy:0x007f887ddb9c98>"], ["resource_type", "Organisation"], ["resource_id", 1]] 

役割は配列ではありません。それは特定の役割でなければなりません。だから、私の次の推測では、今ある私のユーザーのインデックスにリンクにロールを追加してみてくださいすることです。

<%= link_to 'Remove role', assign_role_path(user, role), method: :delete, data: { confirm: 'Are you sure?' } %> 

はその後、私は私を破壊アクションでassigned_roleラインを書き換えることができる方法についてのアイデアを必要とするので、それ私はユーザーから割り当てを解除しようとしている役割を知っています。

私はそれを行うための良いアイデアはありません。

# role = AppRole.find(params[:roles]) 
assigned_role = user.role 
# user_roles = user.roles 

この考えを推進するのに役立つアイデアはありますか?

+0

を使用する必要があります

ActiveRecord::RecordNotFound: Couldn't find User with 'id'= 

で失敗

User.find(nil) 

しようとしている

あなたは 'index'方法を示してくださいだろうか? – idej

+0

ログには何が表示されますか? – Marcus

+0

'user = User.find(params [:users])'の直前に 'puts params'であなたのparamsハッシュを表示します。私の推測では、 'user = User.find(params [:id])'または 'user = User.find(params [:user_id])'を使用する必要があります。 – Magnuss

答えて

1

ただ1人のユーザーの役割を破壊しようとしていますね。 だから、

params[:users] 

は複数で、とはならない:ユーザーがのparamsのための有効なキーではありません。あなたは基本的にあなたはおそらく

User.find(params[:id]) 
+0

こんにちはEric。いいえ、私はユーザーから役割を削除するために探しています。私はassign roles createアクションでロールを割り当てることができます。私は現在、ユーザーから割り当てられた役割を削除する方法を探しています(ユーザー自身ではありません) – Mel

+1

編集ありがとう。エラーメッセージとバックトレース全体がありますか?あなたのコードはどの行で例外を発生させますか? 'ID '='を持つユーザーが見つかりませんでした。あなたのコードで役割を削除しようとしてもできません。 params [:users]はまだRailsにとって珍しいようです。 –

関連する問題