2014-11-05 20 views
0

延長我的下一個定義:before_action從超

的ApplicationController:

before_action :set_resource, only: [:edit, :update, :destroy] 
... 
private 

def set_resource 
... 

OtherController < ApplicationController 

    before_action :set_resource, only: [:new_action1, :new_action2] 
    ... 
    def new_action1 
    .... 
    def new_action2 

我希望set_resource方法之前行動edit, update, destroy,new_action1, new_action2被調用,但它的權利僅適用於方法:edit, update, destroy

+0

那麼你的問題實際上是什麼? – twonegatives 2014-11-05 08:43:31

+0

原因:set_resource未被調用:new_action1,:new_action2 \ – davydes 2014-11-05 08:45:42

+0

@DavydovEugene您已定義:set_resource方法爲private,因此它可以位於應用程序控制器中。嘗試刪除'私人' – Gowri 2014-11-05 09:36:20

回答

0

今天我面臨幾乎相同的問題,同時爲Redm編寫插件國家統計局。 有一個在管理平臺與回調的issues_controller:

OtherController < ApplicationController 
    before_filter :find_issue, :only => [:show, :edit, :update] 
    before_filter :authorize, :except => [:index] 
end 

我添加模塊另一個動作正在被納入issues_controller:對於合併行動

module IssuesControllerPatch 
    def self.included(base) 
    before_filter :find_issue, :only => [:show, :edit, :update, :merge] 
    end 
end 

這裏發生了什麼事,新添加過濾器被稱爲授權方法,從而失敗的授權。

爲了解決這個問題,我推翻授權方式類似:

module IssuesControllerPatch 
    def self.included(base) 
    base.send(:include, InstanceMethods) 

    base.class_eval do 
     alias_method :default_authorize, :authorize 
     alias_method :authorize, :authorize_with_merge 
    end 
    end 

    module InstanceMethods 
    def authorize_with_merge 
     find_issue if params[:action] == "merge" && params[:controller] == "issues" 
     default_authorize 
    end 
    end 
end 

不太優雅,但工程就像一個魅力。這也應該對你有所幫助。