1

次の行に沿った URL スキームを使用して、ネストされたリソースを作成しようとしていますhttp://example.com/username/...

私が現在持っているのはこれです:

ActionController::Routing::Routes.draw do |map|
  map.home '/', :controller => 'home'

  map.resource :session

  map.resources :users, :has_many => :nodes
  #map.user '/:id', :controller => 'users', :action => 'show', :has_many => :nodes

  map.resources :nodes, :belongs_to => :user
end

これにより、次のような URL が生成されます。

http://example.local/users/username
http://example.local/users/username/nodes

「ユーザー」プレフィックスを回避する方法は私にはわかりません。as: => ''に " " オプションを渡してmap.resourcesも機能せず、名前付きルートは " :has_many" または " :belongs_to" オプションをサポートしていないようです。

map.resources :users「 」をコメントアウトし、「 」行のコメントを外してから、機能map.userしているように見えます…ネストされたリソースに到達するまで。次に、次のエラーを吐き出します。

undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>

この問題はこれまで何度も出てきており、常に「なぜそれをしたいのですか?」という質問に直面しています。反応。率直に言って、Twitter はそれを行い、Facebook はそれを行い、私もそれをやりたいと思っています! ;-D

ユーザー名が組み込みパスと競合しないようにする方法についての一般的な批判については、ユーザー名の最小文字数を 6 文字に設定し、すべての組み込みルート レベル パス セグメントを 5 文字以下にする予定です (つまり、" /opt/..."オプションの場合は " /in/..."、セッション ログインの場合は " " など)。

4

2 に答える 2

2

この質問で述べたように:

Rails リソース ルーティングのデフォルト セグメント名

このプラグインを使用できるはずです:

http://github.com/caring/default_routing

または、次のような方法で手動で指定することもできます

map.users     '/users',     :controller => 'users', :action => 'index',
  :conditions => { :method => :get }
map.connect   '/users',     :controller => 'users', :action => 'create',
  :conditions => { :method => :post }
map.user      '/:id',       :controller => 'users', :action => 'show',
  :conditions => { :method => :get }
map.edit_user '/:id/edit',  :controller => 'users', :action => 'edit',
  :conditions => { :method => :get }
map.new_user  '/users/new', :controller => 'users', :action => 'new',
  :conditions => { :method => :get }
map.connect   '/:id', :controller => 'users', :action => 'update',
  :conditions => { :method => :put }
map.connect   '/:id', :controller => 'users', :action => 'destroy',
  :conditions => { :method => :delete }

map.resources :nodes, :path_prefix => '/:user_id', :name_prefix => 'user_'
# to generate user_nodes_path and user_node_path(@node) routes

それまたはそのようなものは、あなたが望むものをあなたに与えるはずですmap.resources

map.resources は機能せず、名前付きルートは ":has_many" または ":belongs_to" オプションをサポートしていないようです。

名前付きルートは、URL パスに別のリソース レベルを追加するために使用される has_many をサポートします。例えば

map.resources :users, :has_many => :nodes

、、およびのようなすべてのusers_pathおよびuser_nodes_pathルートを生成します。と同じです/users/users/:id/users/:id/nodes/users/:id/nodes/:id

map.resources :users do |user|
  user.resources :nodes
end

「map.resources :users」をコメントアウトし、「map.user」行のコメントを外してから、ネストされたリソースに到達するまで機能するようにします。次に、次のエラーを吐き出します。

undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>

これmap.resources :users, :has_many => :nodesは、これらのルートを生成するためです。によって生成される名前付きルートは 1 つだけでmap.user '/:id', :controller => 'users', :action => 'show'、それはuser_path.

于 2009-09-25T03:11:52.433 に答える
0

この関連する質問で私の答えを見てください

基本的に:path => ''、識別セグメントを無効にするオプションとして渡すことができます (Rails 3 でのみテスト済み)。ただし、これは他のルーティング パターンと衝突する可能性があることに注意してください。そのため、配置する場所と方法に注意してください。

于 2011-06-01T13:17:24.207 に答える