2009-07-03 7 views
1

Wie schreibe ich eine Route, die einen Pfad wie diesen abbildet?Verschachteltes Routing

/Poweruser/bob/article-title

Das ist, was ich bisher habe:

map.resources :users, :as => "powerusers" do |users| 
    users.resources :articles, :as => '' 
end 

Das gibt mir die folgende Route:

/Poweruser /: user_id //: id

Wie werde ich die doppelten Backslah loswerden?/powerusers/admin // erster Artikel?

Mit freundlichen Grüßen. Asbjørn Morell

Antwort

4

Ok, wenn Sie die verschachtelte Zwischenressource (/ articles) nicht wollen, würde ich die map.resources überhaupt nicht verwenden.

Versuchen:

map.connect '/powerusers/:user_id/:article_title', :controller => 'articles', :action => 'view_by_title' 
+0

Gibt mir: /powerusers/:user_id/admin/:id(.:format) {: controller => "Artikel ",: action =>" show "}:/ – atmorell

+0

Süße :) Vielen Dank. – atmorell

+0

Ich bin froh, dass ich helfen konnte! –

1

Wenn ich hinzufügen ...

map.resources :users, :as => "powerusers" do |users| 
    users.resources :entries, :as => 'article-title' 
    end 

ich die folgenden Routen, die den einen sind Sie wollen ...

(Substitute "Artikel" für "Einträge" für Ihre Situation.)

   GET /powerusers(.:format)         {:controller=>"users", :action=>"index"} 
       POST /powerusers(.:format)         {:controller=>"users", :action=>"create"} 
       GET /powerusers/new(.:format)        {:controller=>"users", :action=>"new"} 
       GET /powerusers/:id/edit(.:format)      {:controller=>"users", :action=>"edit"} 
       GET /powerusers/:id(.:format)        {:controller=>"users", :action=>"show"} 
       PUT /powerusers/:id(.:format)        {:controller=>"users", :action=>"update"} 
       DELETE /powerusers/:id(.:format)        {:controller=>"users", :action=>"destroy"} 
    user_entries GET /powerusers/:user_id/article-title(.:format)   {:controller=>"entries", :action=>"index"} 
       POST /powerusers/:user_id/article-title(.:format)   {:controller=>"entries", :action=>"create"} 
new_user_entry GET /powerusers/:user_id/article-title/new(.:format)  {:controller=>"entries", :action=>"new"} 
edit_user_entry GET /powerusers/:user_id/article-title/:id/edit(.:format) {:controller=>"entries", :action=>"edit"} 
    user_entry GET /powerusers/:user_id/article-title/:id(.:format)  {:controller=>"entries", :action=>"show"} 
       PUT /powerusers/:user_id/article-title/:id(.:format)  {:controller=>"entries", :action=>"update"} 
       DELETE /powerusers/:user_id/article-title/:id(.:format)  {:controller=>"entries", :action=>"destroy"} 
+0

Dies ist die URL, die ich suche: /powerusers/:user_id/:id(.:format) Ich möchte den zweiten Controller-Namen ausblenden - Artikel-Titel in Ihrem Beispiel – atmorell

1

Anstatt zu verschachteln, würde das funktionieren?

Ich denke, es wird nicht nur ein schneller Test würde sagen besser :)

+0

Probieren Sie es einfach aus und das Ergebnis ist/powerusers /: user_id/articles /: id (.: format) {: controller => "Artikel",: action => "show"} Ich versuche den Artikel von der URL loszuwerden. – atmorell

+0

Ich werfe nur Ideen hier (nicht, dass ich weiß, worüber ich rede). Hast du es versucht: as => nil statt: as => ''? –

+0

np :) Jep, versucht mit: as => nil/false. – atmorell

Verwandte Themen