URL路由中带有点名称空间栏3.1的点 [英] dots in URL routes with namespace rails 3.1
问题描述
我在 routes.rb
root :to => "posts#index"
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :users, :only => :show
resources :boards
resources :posts do
resources :comments
end
namespace :users do
resources :posts do
get :posts, :on => :member
end
resources :boards do
get :boards, :on => :member
end
end
耙道:
boards_users_board GET /users/boards/:id/boards(.:format) {:action=>"boards", :controller=>"users/boards"}
users_boards GET /users/boards(.:format) {:action=>"index", :controller=>"users/boards"}
POST /users/boards(.:format) {:action=>"create", :controller=>"users/boards"}
new_users_board GET /users/boards/new(.:format) {:action=>"new", :controller=>"users/boards"}
edit_users_board GET /users/boards/:id/edit(.:format) {:action=>"edit", :controller=>"users/boards"}
users_board GET /users/boards/:id(.:format) {:action=>"show", :controller=>"users/boards"}
如果我将此链接放入2个参数:
If I put this link with 2 parameters:
<% @posts.each do |post| %>
<%= link_to post.board.name, users_board_path(post.user, post.board) %>
<% end %>
下一个带点号的网址:
http://localhost:3000/users/boards/hyperrjas.board-2
- hyperrjas是我用slug:username放置的user_id.
- 我使用名称空间是因为它们是嵌套资源,并且我有一个供用户使用的面板.
我的问题是:如何在生成的url中更改斜杠/的点?的外观和工作方式如下:
My question is: How can I change the dot for slash / in my generated url? should look and work as follows:
http://localhost:3000/users/boards/hyperrjas/board-2
推荐答案
将用户路线移动到嵌套路线的下方. Rails将首先捕捉"上层路线.
Move the user routes below the nested ones. Rails will "catch" the upper routes first.
您的问题实际上与路线生成(以及一些订购)有关.如果需要访问用户面板,则不需要namespaced
路由,而需要nested
路由.
Your problem is really with the route generation (and some ordering too). If you need to access the users boards, you don't need a namespaced
route but a nested
one.
如果需要/users/:user_id/boards
和/users/:user_id/boards/:id
,则需要嵌套的路由.请注意,在第二条路线中,有2个参数(user_id
和id
).如果使用命名空间路由,则仅需要"一个(板id
).第二个参数是格式.请注意,在耙路径的输出中,您仅需要" 1个参数.
If you need /users/:user_id/boards
and /users/:user_id/boards/:id
you'll need the nested route. Notice that in the second route there are 2 params (user_id
and id
). If you use a namespaced route, you'll only "need" one (the board id
). The second argument would be the format. Notice that in the output from rake routes you only "need" 1 param.
尝试下一条路线,看看是否可行.
Try the next route to see if it works.
resources :users do
# This will give you /users/:user_id/posts
# and /users/:user_id/posts/:post_id
# among others
resources :posts
end
这篇关于URL路由中带有点名称空间栏3.1的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!