ruby - Rails route going to wrong controller action only when there is a rescue_from -
i trying add simple api existing rails 3.2 web app (using this guide). running issue route executing existing action instead of new 1 if try rescue_from exception. here's code (obviously not real code, keeps essential elements):
# config/routes.rb resources :merchants, only: [:index] # existing route # rake routes generates: goodies /goodies(.:format) goodies#index # http://railscasts.com/episodes/350-rest-api-versioning class apiconstraints def initialize(options) @version = options[:version] @default = options[:default] end def matches?(req) req.headers['accept'].include?("application/vnd.my_app.v#{@version}") || @default end end namespace :api, defaults: { format: 'json' } scope module: :v1, constraints: apiconstraints.new(version: 1) resources :goodies, only: [:index] # new route end end # rake routes generates: api_goodies /api/goodies(.:format) api/v1/goodies#index {:format=>"json"} .
# app/controllers/goodies_controller.rb class goodiescontroller < applicationcontroller def index @goodies = goody.order(:name) end end .
# app/controllers/api/v1/base_controller.rb class api::v1::basecontroller < actioncontroller::metal include actioncontroller::rendering include actioncontroller::mimeresponds include abstractcontroller::callbacks append_view_path "#{rails.root}/app/views" before_filter :authenticate rescue_from notauthenticated, :deny_access # works if comment-out line private def authenticate # fail notauthenticated if not authenticated end def deny_access render json: { error_message: 'forbidden' }, status: 403 end end .
# app/controllers/api/v1/goodies_controller.rb class api::v1::goodiescontroller < api::v1::basecontroller def index @goodies = goody.order(:name) end end here scenarios when send get my_app.dev/api/goodies request has accept header value set application/vnd.my_app.v1:
- (with
rescue_from)- without auth creds - results view used
goodies#indexinstead of 1 usedapi/v1/goodies#index - with auth creds - same
- without auth creds - results view used
- (without
rescue_from)- without auth creds - standard development rails 500 error page showing
notauthenticatederror raised - with auth creds - results view used
api/v1/goodies#index
- without auth creds - standard development rails 500 error page showing
for scenario #1, know it's not using wrong view because if put fail in api/v1/goodies#index, still results goodies#index.
i don't think relevant, i'm using *.json.jbuilder views.
i saw this post, similar, specific rescue_from. haven't tried op's comment, don't think should have change controller name make work.
Comments
Post a Comment