我在向控制器添加新方法时遇到问题,以下是详细信息:
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
我之前在另一个应用程序中做过这个,我不知道它为什么不在这里工作。
答案 0 :(得分:1)
您无法访问直接传递给flash
的块中的rescue_from
。但是,如果传递异常处理程序方法的符号,则可以访问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_from
上的ActionController Rails Guide部分。