2017-06-19 3 views
0

私の目的は、現在のユーザが管理者ユーザの場合に有効になっているNavbarのUser Adminメニューオプションを持つことです。管理者ユーザはこれを選択することができ、レールのルートであるusers_pathにルーティングされます。rails + devise + cancancan + rolify - ユーザインデックスビューのユーザ情報を表示できません

ユーザーがアプリ/ビュー/ユーザー/ index.htmlの内/users(.:format)のユーザー#インデックス

をGET。 erbは現在ユーザー名と電子メールを表示するテーブルです。最終的な目的は、管理者のチェックボックスを使用して、管理者ユーザーがこれをチェックして、そのユーザーに対して一度送信された役割を「管理者」に変更できるようにすることです。基本的に、他のユーザーを管理するユーザー管理ページ。

しかし、現時点では、表にはユーザー名と電子メールのフィールドだけがテーブルにあります。問題は、この表にデータがないことです。それは空白です。しかし、私はデバッグを使用すると、ページが読み込まれたときにデバッグパネルのすべてのデータを見ることができます。ここで user index page with table empty but debug panel populated with user data

それが現在の設定であるかである:

1)routes.rbを:

class UsersController < ApplicationController 
 
    load_and_authorize_resource 
 
    def index 
 
    @users = User.all 
 
    end 
 
end

users_controller.rb

devise_for :users resources :users, only: [:index]

2)

class User < ApplicationRecord 
 
    rolify 
 
    devise :database_authenticatable, :rememberable, :trackable, :registerable 
 
    attr_accessor :username, :email, :password, :password_confirmation, :remember_me, :firstname, :lastname 
 
    after_create :assign_default_role 
 
    def assign_default_role 
 
    self.add_role(:user) if self.roles.blank? 
 
    end 
 
end

5)application_controller.rb

:10

3)

<table id="user_list" class="display table table-striped table-sm table-hover" cellspacing="0" width="100%" > 
 
    <thead> 
 
    <tr> 
 
     <th>Username</th> 
 
     <th>Email</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
    <% @users.each do |user| %> 
 
     <tr> 
 
     <td><%= user.username%></td> 
 
     <td><%= user.email %></td> 
 
     </tr> 
 
    <% end %> 
 
    </tbody> 
 
</table>

4)ユーザモデルindex.html.erb

class ApplicationController < ActionController::Base 
 
    protect_from_forgery with: :exception 
 
    before_action :authenticate_user! 
 
    protected 
 
    def configure_permitted_parameters 
 
    added_attrs = %i[username email password password_confirmation remember_me] 
 
    devise_parameter_sanitizer.permit :sign_in, keys: added_attrs 
 
    end 
 
end

6)カンカン能力クラス

class Ability 
 
    include CanCan::Ability 
 
    def initialize(user) 
 
    if user.has_role? :admin 
 
     can :manage, :all 
 
    else 
 
     can :read, :all 
 
    end 
 
    end 
 
end

index.html.erbに表示するユーザデータを妨げているものを任意のアイデア?

+0

デバッグツール 'pry'または' byebug'を使って '@ users'変数を検査してみてください。 '@ users.size'はあなたに何を与えるのでしょうか? – max

+0

@users。3つのユーザー行がデータベースにあるので、sizeは3を返します。私はRailsデバッガヘルパーを使用しており、これはページ上のデバッグパネルにデータをプルします。私はテーブルのユーザ名と電子メールフィールドに対応するデータを入力できません – wbanguna

答えて

0

問題が見つかりました。私はユーザーモデルでattr_accessorを使用し、user_controllerでは強力なパラメータを使用していました。

attr_accessorは廃止され、Rails 4の強力なパラメタに置き換えられました。両方を必要としません。 attr_accessorを削除すると、データはユーザーインデックスビューのテーブルに入力されました。

関連する問題