Rails命名路由在开发中起作用,但在生产中不起作用 [英] Rails namespaced routes work in development but not production
问题描述
我正在尝试将一些路由嵌套在名称空间account下.
I'm trying to nest some routes under the namespace, account.
我希望使用/account/users
和/account/users/5/edit
在routes.rb中:
In routes.rb:
namespace :account do
resources :users do
member do
put 'generate_api_key'
end
collection do
post 'api_key'
end
end
end
我的控制器没有命名空间,也没有放置在任何其他目录中.
My controllers are not namespaced or put them in any different directory.
/app
/controllers
accounts_controller.rb
users_controller.rb
在我的开发环境中,它工作正常,但在生产中,我得到404对任何/account/users...
路径的响应(顺便说,这些路径仍然可以正确生成:new_account_users_path
,edit_account_user_path
等).
In my development environment this is working fine, but in production I get 404 responses to any of the /account/users...
paths (which, by the way, are all still generated correctly: new_account_users_path
, edit_account_user_path
, etc).
rake routes
在两种环境中都会生成相同的输出.这是相关的位:
rake routes
generates the same output in both environments. Here is the relevant bit:
generate_api_key_account_user PUT /account/users/:id/generate_api_key(.:format) {:action=>"generate_api_key", :controller=>"account/users"}
api_key_account_users POST /account/users/api_key(.:format) {:action=>"api_key", :controller=>"account/users"}
account_users GET /account/users(.:format) {:action=>"index", :controller=>"account/users"}
POST /account/users(.:format) {:action=>"create", :controller=>"account/users"}
new_account_user GET /account/users/new(.:format) {:action=>"new", :controller=>"account/users"}
edit_account_user GET /account/users/:id/edit(.:format) {:action=>"edit", :controller=>"account/users"}
account_user GET /account/users/:id(.:format) {:action=>"show", :controller=>"account/users"}
PUT /account/users/:id(.:format) {:action=>"update", :controller=>"account/users"}
DELETE /account/users/:id(.:format) {:action=>"destroy", :controller=>"account/users"}
鉴于路由似乎在/account
子目录中寻找Users
控制器,我想我的问题是为什么这在开发中会起作用?
Given that the routes seem to look for the Users
controller in the /account
subdirectory, I suppose my question is why does this work in development?
生产是:
- 导轨3.0.7
- 旅客
- Apache
发展是:
- 导轨3.0.7
- 杂种
感谢您对此的看法.
推荐答案
如果要这样命名,Rails要求控制器使用正确的路径,例如app/controllers/account/users_controller.rb
.如果您不想这样做,请改用scope
:
If you're namespacing like this, Rails requires the controllers to be at their correct paths, such as app/controllers/account/users_controller.rb
. If you don't want to do this, then use scope
instead:
scope :path => "account" do
resources :users
end
这篇关于Rails命名路由在开发中起作用,但在生产中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!