2014-05-24 142 views
0

行動之前使用所以目前我有一個before_action設置一個檢查,如果用戶登入後,纔可以做某些事情......軌道4,與參數

before_action :signed_in_user, only: [:edit, :update, :index, :destroy, :show] 

... 

def signed_in_user 
    unless signed_in? 
    store_location 
    redirect_to signin_url 
    flash[:warning] = "Please sign in." 
    end 
end 

然而,有一件事我要更改是當用戶嘗試訪問特定操作而未登錄時,我希望Flash消息的內容比「請登錄」更有用。即「請登錄[做某個動作]」。

我該如何修改我已經有這個功能?

回答

0

只需使用action_name

def signed_in_user 
    unless signed_in? 
    store_location 
    redirect_to signin_url 

    if %w[edit update].include? action_name 
     flash[:warning] = "Please sign in to edit records." 
    else 
     flash[:warning] = "Please sign in." 
    end 
    end 
end