Ruby on Rails 5设计自定义字段不起作用

Ruby on Rails 5设计自定义字段不起作用

问题描述:

我遇到了设计自定义字段Ruby on Rails 5.0的问题。我正在使用设计来处理用户帐户维护。我已成功将名为admin_user的自定义字段添加到我的用户模型中。该字段是一个布尔字段。我创建一个会话变量来保存用户是否是我的应用程序控制器中的回调函数中的管理员用户。
这个想法是,当用户登录时,如果他们是管理员用户,他们将看到管理菜单。如果他们是普通用户,他们不会。这似乎并不奏效。我担心数据库不会将admin用户存储为布尔型字段。该值在数据库中显示为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?代替。我已经尝试过,但不幸的是它没有工作。
如果有人能够帮助,甚至指向正确的方向,我会非常感激。
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 %> 

部分数据库的表列表

id = 1 
email = asas.com 
admin_user = t 

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

数据库表中列出

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 

我非常等等rry民族在我写下这个问题后,一个可能的解决方案就在我身上显现出来。我应该在发布之前测试过,但如果我没有发布这个问题,我可能找不到解决方案。使用下面的代码修复了这个问题。这也意味着我不需要应用程序控制器中的回调方法。我认为积极的一面,写出这个问题帮助我解决了这个问题。我希望通过在此发布它可以帮助其他人。

<% 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 %>