[英]Problems with CanCan Methods with Rails3 / Devise
兩天后,我無法自行解決此問題。 看起來應該很簡單,但是我缺少了一些東西。 我正在用Posts and Authors創建一個簡單的博客。 作者有一個布爾admin列。
現在出現錯誤的行是我檢查權限以在帖子中顯示編輯按鈕的地方。當前錯誤是:
Posts#show中的NoMethodError
在第18行出現的位置顯示... / posts / show.html.erb:
#的未定義方法`stringify_keys'
posts / show.html.rb
<% if @author.can? :update, @post %>
<p><%= link_to 'Edit', edit_post_path(@post), :class => 'btn' %> <%= link_to 'Destroy', @post, confirm: 'Are you sure?', method: :delete %></p>
<% end %>
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
helper_method :current_author
def current_user
@current_ability ||= Author.new(current_author)
end
end
能力
class Ability
include CanCan::Ability
def initialize(author)
author ||= Author.new # guest user (not logged in)
if author.admin?
can :manage, :all
else
can :read, :all
end
end
end
另外,據我所知,CanCan正確包含在gem文件中。
兩件事情。
首先,您需要在控制器中有一個can_can依賴的current_user
方法。 如果您沒有一個,可以
current_user
您current_whatever
方法或 @ability = Ability.new(current_whatever)
然后打電話給您can?
在您認為的生成能力上(例如@ability.can? :edit, @post
)。 其次,您的Ability
在第4行和第5行都使用current_author
,但是您的initialize
方法中沒有current_author
。 不過,您有author
。 如果該功能的初始化程序沒有可用的Author
對象,則使用非持久性作者(而不是AuthorAbility,除非AuthorAbility是current_user
返回或在您的功能中進行initialize
東西作為參數)。 像這樣:
class Ability
include CanCan::Ability
def initialize(author)
author ||= Author.new # guest user (not logged in)
if author.admin?
can :manage, :all
else
can :read, :all
end
end
end
根據評論進行編輯以使其更簡單:
理想情況下,將current_user
方法放入應用程序控制器中,並在視圖中將其用作幫助程序(因為您可能希望基於登錄的用戶有條件地在視圖中顯示/隱藏/更改內容)。
class ApplicationController < ActionController::Base
helper_method :current_user
def current_user
# return the currently logged in user record
end
end
如果這對您來說是新手,我建議您看一下身份驗證寶。 Devise也對此進行了介紹,而authlogic在其使用方法和示例應用程序中對此進行了描述。 如果您要從頭開始進行身份驗證,則只需根據會話返回用戶。
編輯2.您實際上需要了解您的操作,恕我直言,目前情況並非如此。 你在這里有點混亂;-)
問題1: current_user
需要返回當前登錄的作者/用戶 (不是abilty,fallback用戶或其他身份),如果沒有作者登錄,則返回nil
。因此,例如,您可以在視圖中執行<% if current_user %>
。 @current_ability ||= Author.new(current_author)
是完全錯誤的。 來自能力類的后退需要保留在能力類中,因為cancan的方法只能應用於對象,而不能應用於nil
。 因此,如果您具有author ||= Author.new
,即使沒有作者登錄,也請確保存在一個對象(在這種情況下, current_author
返回nil
)。
問題2: helper_method :current_author
實際上不執行任何操作,因為您的應用程序控制器中沒有current_author
方法。 您需要以某種方式定義current_author
。
問題3:在您看來,您在打電話can?
在一個Author
實例上,這是錯誤的。 can?
是一種Ability
的方法。 因此,您需要使用@my_ability.can?
其中@my_ability是例如Ability.new(Author.first)
的實例。 如果您需要使用多種能力或自定義某些內容,則使用此方法,此處不是這種情況,那么使用can?
直接沒有接收者(例如@author.can?
)。
為了進行測試,我將創建以下內容:
class ApplicationController < ActionController::Base
helper_method :current_user # as defined below...
def current_user
# Return a static user here, for testing purposes,
# like @current_user = User.first or Author.first
end
end
因此,您的current_user
返回一個有效用戶(不過,我希望您至少需要在數據庫中存儲一個用戶),然后才能解決能力問題。 如果您的能力有效,則可以實施身份驗證。 作為一個初學者,我要么堅持authlogic要么設計一個 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.