嵌套的名称空间路由转到错误的控制器 [英] nested namespace route going to wrong controller

查看:94
本文介绍了嵌套的名称空间路由转到错误的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Rails 3.0.7,我正在为我们的应用程序创建一个API,并且具有以下设置:

Using Rails 3.0.7, I'm creating an API for our app, and I have this setup:

routes.rb

routes.rb

  namespace :api do
    namespace :v1 do
      match "connect" => "users#login", :via => :post
      match "disconnect" => "users#logout", :via => :post
      resources :users
      match "users/:id/foos" => "foos#list", :via => :get
      match "users/:id" => "users#update", :via => :put
      match "foos/:id/bars" => "bars#list_by_foo", :via => :get
      match "foos/:id" => "foos#show", :via => :get, :constraints => { :id => /\d+/ }
      match "bars/:id" => "bars#show", :via => :get
    end
  end

  # other routes here e.g.
  match "users/find" => "users#find_by_name", :via => :get
  match "users" => "users#create", :via => :post

然后我有常规的app/controllers/application_controller.rbapp/controllers/users_controller.rb文件以及定义如下的app/controllers/api/v1/application_controller.rbapp/controllers/api/v1/users_controller.rb文件:

And then I have my regular app/controllers/application_controller.rb and app/controllers/users_controller.rb files as well as my app/controllers/api/v1/application_controller.rb and app/controllers/api/v1/users_controller.rb files that are defined like the following:

class Api::V1::ApplicationController < ApplicationController
  before_filter :verify_access

  def verify_access
    # some code here
  end
end

class Api::V1::UsersController < Api::V1::ApplicationController
  skip_before_filter, :except => [:show, :update, :delete]
end

在一切似乎都正常之前,直到我覆盖了由UsersController和Api :: V1 :: UsersController共享的方法为止-现在,即使我正在通过api/v1/用户路由.

And before everything seemed to be working right until I overrode a method that is shared by both UsersController and Api::V1::UsersController -- and now it seems like everything is pointing to UsersController even though I'm accessing through the api/v1/users route.

我正竭尽全力试图解决这个问题.有什么建议?谢谢. PS-随意发表评论,不管我忽略了我不应该成为的习惯,还是其他我可能搞砸了的东西:)

I'm at my wit's end trying to figure it out. Any suggestions? Thanks. PS - feel free to comment with whatever conventions I'm ignoring that I shouldn't be or other things I might have messed up :)

推荐答案

Rails有点令人困惑,但是我遇到了类似的问题.您可以采取以下步骤来确保您不会丢失任何小代码问题. (最终使我发现了命名空间控制器中的语法错误).

Rails is a bit confusing, but I had a similar problem. Here's some steps you can take to make sure you're not missing any small code issues. (this eventually led me to discover a syntax bug in the namespaced controller).

  1. 运行bundle exec rake routes以生成哪些路由链接到哪个控制器和动作的列表.如果这很好,请转到步骤2.否则,请修复您的路由文件,然后重试. (有关这方面的很多很好的教程,因此我将不做详细介绍)
  2. 进入rails控制台,然后加载控制器类.如果它不起作用,则可能是您发现了语法错误.这是我尝试加载Api::V2::CampaignsController时在控制台上发生的事情.

  1. run bundle exec rake routes to generate a list of what route links to what controller and action. If this is good, then move to step 2. If not, fix your routes file and try again. (many good tutorials on this, so I won't go into detail)
  2. Go into the rails console, and just load the controller class. If it doesn't work, you may have discovered a bug in syntax. Here's what happened on console when I tried to load the Api::V2::CampaignsController.

irb> Api :: V2 :: CampaignsController

irb> Api::V2::CampaignsController

=> CampaignsController

=> CampaignsController

注意:Rails将所有请求都定向到错误的控制器(基于Rails的幻想逻辑来加载控制器类).它应该转到Api :: V2 :: CampaignsController,但它正在加载CampaignsController.

Note: Rails is directing all requests to the wrong controller (based on Rails' fancy logic to load controller classes). It should goto Api::V2::CampaignsController, but instead it is loading CampaignsController.

您还可以使用以下命令在控制台中进行验证:

You can also verify it in the console with:

> app.get '/api/v2/campaigns.json'
> app.controller.class
=> CampaignsController
# This is not the expected controller. 

这最终是我从Api :: V2 :: CampaignsController扩展的类中的语法问题.

This ended up being a syntax problem in a class I was extending from the Api::V2::CampaignsController.

这有点令人难以置信,但是希望这对其他人有帮助.

It was a bit mind-boggling, but hope this helps someone else.

这篇关于嵌套的名称空间路由转到错误的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆