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