2017-07-16 65 views
0

目前正在構建一個教育應用程序,我有一個管理模型和一個用戶模型。您如何使用Devise幫助器方法,以便某些控制器操作僅限於某個用戶?

我只想讓管理員訪問導致創建,編輯和刪除請求的鏈接。

我目前使用的是<% if user_signed_in? %>,只允許登錄用戶訪問鏈接,但自從我添加了管理員角色後,我只希望管理員能夠訪問鏈接。

我沒有做任何事情給我的管理員或用戶控制器,只是產生了必要的模型和控制器。

ActiveRecord::Schema.define(version: 20170714091732) do 

    create_table "admins", 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.index ["email"], name: "index_admins_on_email", unique: true 
    t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true 
    end 

    create_table "courses", force: :cascade do |t| 
    t.string "title" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "lessons", force: :cascade do |t| 
    t.string "title" 
    t.text "content" 
    t.integer "course_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    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.index ["email"], name: "index_users_on_email", unique: true 
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 
    end 

end 
+0

你有一些用戶表中的角色作爲管理員? – Pavan

+0

不,我不知道。我選擇了創建兩個用戶模型的路徑,一個是管理員,另一個是用戶,所以我沒有任何指示任何用戶角色的數據庫表列。 – yamaoka

+0

這些模型之間有任何關聯嗎? – Pavan

回答

1

如果你設置了設計一種管理模式正常,你應該能夠過濾器之前添加到您的控制器操作是這樣的:before_action: authenticate_admin! 你也應該有機會獲得其他幫助像admin_signed_in?

相關問題