2016-07-22 119 views
0

我遇到了設計自定義字段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 

回答

1

我非常等等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 %>