2016-07-22 32 views
0

deviseカスタムフィールドRuby on Rails 5.0に問題があります。私はユーザーアカウントのメンテナンスを処理するためにdeviseを使用しています。私は正常に私のユーザーモデルにadmin_userというカスタムフィールドを追加しました。このフィールドはブール型フィールドです。私はセッション変数を作成して、ユーザーがアプリケーションコントローラのコールバック関数の管理ユーザーかどうかを保持します。
アイデアは、ユーザーが管理者ユーザーであればログインすると、管理者メニューが表示されるということです。彼らが普通のユーザーであれば、そうはなりません。これはうまくいかないようです。私は、データベースが管理者ユーザーをブール値フィールドとして格納していないと懸念しています。値はデータベースにtまたはfとして表示されるので、値がテキスト値であるか、実際はブール値かどうかはわかりません。
私が次のことを試しても何の問題もありません。システムは、すべてのユーザーが管理ユーザーではないと考えているようです。誰が何が間違っているかについてのアイデアはありますか?私がやっている次に基づいて
は正しいはずです:
how to add admin with devise in RORRuby on Rails 5のカスタムフィールドがうまく動作しません。

Set a session variable in devise on sign in

私はRubyコードのcurrent_user.admin_userを使用してもらえますか?代わりに。私はこれを既に試しましたが、残念ながらそれはうまくいきませんでした。
誰かが助けてくれたり、正しい方向で私を指摘しても、私は非常に幸せになるでしょう。私は非常にそうだ

create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0,  null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at",        null: false 
    t.datetime "updated_at",        null: false 
    **t.boolean "admin_user",    default: false** 
    t.integer "addresses_id" 
    t.integer "sales_orders_id" 
    t.integer "payments_id" 
    t.index ["addresses_id"], name: "index_users_on_addresses_id" 
    t.index ["email"], name: "index_users_on_email", unique: true 
    t.index ["payments_id"], name: "index_users_on_payments_id" 
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 
    t.index ["sales_orders_id"], name: "index_users_on_sales_orders_id" 
    end 

コントローラコード

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    # GET /categories 
    # GET /categories.json 
    def index 
    @categories = Category.all 
    end 
    before_filter :load_initial_data 

protected 
    def after_update_path_for(resource) 
    session[:admin_usr] = current_user.admin_user 
    user_path(resource) 
    end 
    def load_initial_data 
    @categories = Category.all 
    end 
end 

答えて

1

をリスト

id = 1 
email = asas.com 
admin_user = t 

id = 2 
email = [email protected] 
admin_user = f 

データベースのテーブルをリスト
HTML

<% if session[:admin_usr] %> 
    <div id="adminMenu" name="adminMenu" class="dropdown"> 
    <button class="dropbtn">Admin</button> 
    <div class="dropdown-content"> 
     <a href="/brands">Brand</a> 
     <a href="/products">Product</a> 
     <a href="/categories">Category</a> 
    </div> 
    </div> 
<% else %> 
    <div id="profileMenu" name="profileMenu" class="dropdown"> 
    <button class="dropbtn">Profile</button> 
    <div class="dropdown-content"> 
     <a href="/addresses">Addresses</a> 
     <a href="/payments">Payments</a> 
     <a href="/payments">Orders</a> 
    </div> 
    </div> 
<% end %> 

部分的なデータベースの表私は質問を書いた後、可能な解決策が私にもたらされました。私は投稿する前にこれをテストしていたはずですが、質問を投稿していない場合は解決策が見つかりませんでした。次のコードを使用すると問題が解決されます。つまり、アプリケーションコントローラにコールバックメソッドが必要ないということです。私は正の面で、問題を書いて解決するのを助けました。私は他人を助けることをここに掲載することを願っています。

<% if current_user.admin_user %> 
    <div id="adminMenu" name="adminMenu" class="dropdown"> 
    <button class="dropbtn">Admin</button> 
    <div class="dropdown-content"> 
     <a href="/brands">Brand</a> 
     <a href="/products">Product</a> 
     <a href="/categories">Category</a> 
    </div> 
    </div> 
<% else %> 
    <div id="profileMenu" name="profileMenu" class="dropdown"> 
    <button class="dropbtn">Profile</button> 
    <div class="dropdown-content"> 
     <a href="/addresses">Addresses</a> 
     <a href="/payments">Payments</a> 
     <a href="/payments">Orders</a> 
    </div> 
    </div> 
<% end %> 
関連する問題