我希望我的艺术家链接看起来像这样:
http://admin.foobar.com/artists/123
http://www.foobar.com/123
我的Routes
设置如下:
class AdminSubDomain
def matches?(request)
whitelists = IpPermission.whitelists
if whitelists.map { |whitelist| whitelist.ip }.include? request.remote_ip
request.subdomain == 'admin'
else
raise ActionController::RoutingError.new('Not Found')
end
end
end
Foobar::Application.routes.draw do
constraints AdminSubDomain.new do
..
resources :artists, :only => [:index, :show], :controller => 'admin/artists'
end
get ':id' => 'artists#show', :as => 'artist' do
..
end
end
Rake routes
返回:
artist GET /artists/:id(.:format) admin/artists#show
artist GET /:id(.:format) artists#show
目前,<%= link_to 'Show', artist_path(artist, :subdomain => :admin) %>
指向:http://admin.foobar.dev:3000/123
。
它应该如下所示:http://admin.foobar.dev:3000/artists/123
我做错了什么?
答案 0 :(得分:1)
您对两个路线使用了相同的名称(artist
),因此当您致电artist_path
时,您将获得您定义的最后一个,即:get ':id' = 'artists#show', :as => 'artist' do ...
。< / p>
使用管理员路径的其他名称来区分它:
constraints AdminSubDomain.new do
..
resources :artists, :only => [:index, :show], :controller => 'admin/artists', :as => 'admin_artists'
end
然后你可以用<%= link_to 'Show', admin_artist_path(artist, :subdomain => :admin) %>
来调用它。