2012-12-25 46 views
0

我做了TODO清單的申請。與設計的待辦事項清單

我安裝裝置和後要定義兩種用戶:

1) admin 
2) worker 

,所以我將創建管理控制器。同時,我創建了user_controller。

我要強制註冊,爲了讓工人更新他的任務(完成與否),所以我嘗試:

class WorkersController < ApplicationController 
    before_filter :authenticate_user! 

PS,我的模型的名字是user.rb:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :lockable, :timeoutable, :confirmable and :activatable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation 
    # attr_accessible :title, :body 
end 

但是:authenticate_user!不工作。即使我退出,我也可以創建一項任務。

之後,我必須知道登錄的用戶的郵件是什麼(爲了更新他的任務)。所以我要寫類似:

def index 
    @email = params[:session][:email] 

,但我得到了一個錯誤:

所有的
NoMethodError in WorkersController#index 

undefined method `[]' for nil:NilClass 
Rails.root: /home/alon/projects/TODOLIST 

Application Trace | Framework Trace | Full Trace 
app/controllers/workers_controller.rb:8:in `index' 

回答

2

首先,電子郵件沒有存儲在會話。 登錄後,Devise有一個輔助方法current_resource(current_user在你的情況下,因爲「資源」一詞被替換爲模型名稱)。 ,你需要使用慘慘https://github.com/ryanb/cancan

您應該使用

@email = current_user.email 

至於允許/ dissalowing一些行動