I am having a problem with devise custom field Ruby on Rails 5.0. I am using devise to handle user account maintenance. I have successfully added a custom field called admin_user to my user model. This field is a Boolean field. I create a session variable to hold whether the user is an admin user in a callback function in my application controller.
The idea is that when a user logs in if they are admin user they will see the admin menu. If they are an ordinary user they won't. This doesn't seem to work. I am concerned that the database is not storing admin user as a boolean field. The value shows up as t or f in the database so I dont know if that means they value is a text value or it is actually a boolean.
No matter what I try the following doesn't work. The system seems to think that all the user are not admin users. Would anyone have any ideas as to what is wrong.
Based on the following what I'm doing should be correct:
how to add admin with devise in ROR
Set a session variable in devise on sign in
Could I use the ruby code current_user.admin_user? instead. I've tried this already but unfortunately it didn't work.
If anyone could help or even point me in the right direction I'd be very greatful.
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 %>
Partial Database Table Listing
id = 1
email = asas.com
admin_user = t
id = 2
email = s@s.com
admin_user = f
Database Table listing
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
Controller Code
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