Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 只有在有救援行动时,轨道路径才会导致错误的控制器动作_Ruby On Rails_Ruby_Ruby On Rails 3.2_Routing - Fatal编程技术网

Ruby on rails 只有在有救援行动时,轨道路径才会导致错误的控制器动作

Ruby on rails 只有在有救援行动时,轨道路径才会导致错误的控制器动作,ruby-on-rails,ruby,ruby-on-rails-3.2,routing,Ruby On Rails,Ruby,Ruby On Rails 3.2,Routing,我正在尝试向现有的Rails3.2Web应用程序添加一个非常简单的API(使用指南)。我遇到了这样一个问题:我的路由正在执行一个现有的操作,而不是我的新操作,但只有当我尝试从异常中营救\u时。下面是代码(显然不是我真正的代码,但保留了基本元素): #app/controllers/goodies_controller.rb 类GoodiesController

我正在尝试向现有的Rails3.2Web应用程序添加一个非常简单的API(使用指南)。我遇到了这样一个问题:我的路由正在执行一个现有的操作,而不是我的新操作,但只有当我尝试
从异常中营救\u时。下面是代码(显然不是我真正的代码,但保留了基本元素):

#app/controllers/goodies_controller.rb
类GoodiesController<应用程序控制器
def索引
@goodies=Goody.order(:name)
结束
结束

#app/controllers/api/v1/base#u controller.rb
类Api::V1::BaseController

#app/controllers/api/v1/goodies_controller.rb
类Api::V1::GoodiesController
以下是我发送的
GET my_app.dev/api/goodies
请求的场景,该请求的
Accept
头值设置为
application/vnd。my_app.v1

  • (使用<代码>从中营救)
  • 没有auth creds-我从
    goodies#index
    使用的视图获得结果,而不是
    api/v1/goodies#index
  • 具有认证证书-相同
  • (没有从
  • 中救援)
  • 没有身份验证凭据-我得到了标准开发Rails 500错误页面,显示出现了
    NotAuthenticated
    错误
  • 使用auth creds-我从
    api/v1/goodies\index
  • 对于场景1,我知道这不仅仅是使用了错误的视图,因为如果我在
    api/v1/goodies#index
    中放入
    fail
    ,我仍然会得到
    goodies#index
    的结果

    我认为这与此无关,但我使用的是
    *.json.jbuilder
    视图


    我看到了一篇帖子,这篇帖子与之类似,但具体针对的是来自
    rescue\u。我还没有试过OP的评论,但我认为我不应该改变我的控制器名称以使其工作。

    @default | | value
    非常可疑。如果给该类一个默认值,它将永远不会尝试计算传入值。您需要
    value | |@default
    @meagar good catch!:)但仍然不能解决我目前的问题…:(
    # config/routes.rb
    
    resources :merchants, only: [:index] # existing route
    # rake routes generates: goodies GET /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' } do
      scope module: :v1, constraints: ApiConstraints.new(version: 1) do
        resources :goodies, only: [:index] # new route
      end
    end
    # rake routes generates: api_goodies GET /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 # it works if I comment-out this 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