,下面是詳細信息:如何增加在軌控制的另一種方法
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
我之前在另一個應用程序中完成了此操作,我不知道它爲什麼不在這裏工作。
哪部分不工作?你是否收到「無路線」錯誤?什麼'耙路線'輸出? – 2013-03-27 04:37:55
我編輯了我的問題,請立即查看 – 2013-03-27 05:12:47
您的錯誤指的是您沒有包含的文件:application_controller.rb。請顯示該文件。 – 2013-03-27 05:18:10