2013-03-27 24 views
0
我有一個問題,同時增加了新的方法到控制器

,下面是詳細信息:如何增加在軌控制的另一種方法

matches_controller.rb

def index 
    @matches = Match.all  
    render rabl: @matches 
end 

def current 
    @matches = Match.find_by_sql("SELECT * FROM `TEST`.`matches` where live=1;")  
    render rabl: @matches 
end 

的routes.rb

resources :matches, defaults: {format: :json}, except: :edit do 
    collection do 
    get :current 
    end 
end 

current.json.rabl

collection @match 
    attributes :id,:live 

瑞克路由

current_matches GET /matches/current(.:format) matches#current {:format=>:json} 
     matches GET /matches(.:format)   matches#index {:format=>:json} 
       POST /matches(.:format)   matches#create {:format=>:json} 
     new_match GET /matches/new(.:format)  matches#new {:format=>:json} 
      match GET /matches/:id(.:format)  matches#show {:format=>:json} 
       PUT /matches/:id(.:format)  matches#update {:format=>:json} 
       DELETE /matches/:id(.:format)  matches#destroy {:format=>:json} 

在日誌中的錯誤

NameError (undefined local variable or method `flash' for #<MatchesController:0x0000000d7b2368>): 
app/controllers/application_controller.rb:6:in `block in <class:ApplicationController>' 

application.rb中

class ApplicationController < ActionController::API 
    include ActionController::MimeResponds 
    include CanCan::ControllerAdditions 

    rescue_from CanCan::AccessDenied do |exception| 
    flash[:error] = exception.message 
    puts exception.message 
    redirect_to root_url 
    end 

    def log_exception(exception) 
    logger.error(exception.message) 
    logger.error(exception.backtrace.join("\n")) 
    end 
end 

我之前在另一個應用程序中完成了此操作,我不知道它爲什麼不在這裏工作。

+2

哪部分不工作?你是否收到「無路線」錯誤?什麼'耙路線'輸出? – 2013-03-27 04:37:55

+0

我編輯了我的問題,請立即查看 – 2013-03-27 05:12:47

+0

您的錯誤指的是您沒有包含的文件:application_controller.rb。請顯示該文件。 – 2013-03-27 05:18:10

回答

1

您不能在直接傳遞給rescue_from的塊中訪問flash。但是,如果傳遞異常處理程序方法的符號,則可以訪問flash。例如:

class ApplicationController < ActionController::API 
    rescue_from CanCan::AccessDenied, :with => :access_denied 

    private 

    def access_denied(exception) 
    flash[:error] = exception.message 
    redirect_to root_url 
    end 
end 

有關詳細信息,請參閱第rescue_fromActionController Rails Guide部分。

+0

thanx但沒有工作:( – 2013-03-27 05:40:06

+0

同樣的確切的錯誤? – 2013-03-27 05:40:57

+0

是同樣的錯誤:( – 2013-03-27 05:43:10

相關問題