0

我希望我的艺术家链接看起来像这样:

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

我究竟做错了什么?

4

1 回答 1

1

您为两条路线使用了相同的名称 ( artist),因此当您调用 时artist_path,您将获得您定义的最后一个名称,即:get ':id' = 'artists#show', :as => 'artist' do ...

为管理路由使用不同的名称来区分它:

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) %>

于 2013-01-03T05:18:46.403 回答