ruby on rails -我如何指向正确的管理艺术家链接



我想让我的艺术家链接看起来像这样:

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

我做错了什么?

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

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

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

相关内容

  • 没有找到相关文章

最新更新