簡體   English   中英

在Ruby on Rails中路由生成的路徑

[英]Routing generated paths in Ruby on Rails

我是使用ruby-on-rails的初學者,我花了最后一個小時嘗試執行以下操作:

我有一個ruby-on-rails應用程序-帶有帖子和類別的博客。 我想要帖子的另一個URL(我希望使用http://localhost:3000/news而不是http://localhost:3000/posts )首先,我嘗試將控制器和類從Posts替換為News ,但是我放棄了(因為煩人的單數復數的事情)。 然后在我map.resources :posts (情況1)替換為

  map.resources :news, :controller => "posts"     #case 2

要么

  map.resources :posts, :as => 'news'             #case 3

如我在一些網站上看到的在routes.rb中。 它也不起作用。

我怎樣才能做到這一點?


編輯:

rake routes的輸出為(僅第一行):

對於情況1和3:

               posts GET    /posts                           {:action=>"index", :controller=>"posts"}
     formatted_posts GET    /posts.:format                   {:action=>"index", :controller=>"posts"}
                     POST   /posts                           {:action=>"create", :controller=>"posts"}
                     POST   /posts.:format                   {:action=>"create", :controller=>"posts"}
            new_post GET    /posts/new                       {:action=>"new", :controller=>"posts"}
  formatted_new_post GET    /posts/new.:format               {:action=>"new", :controller=>"posts"}
           edit_post GET    /posts/:id/edit                  {:action=>"edit", :controller=>"posts"}
 formatted_edit_post GET    /posts/:id/edit.:format          {:action=>"edit", :controller=>"posts"}
                post GET    /posts/:id                       {:action=>"show", :controller=>"posts"}
      formatted_post GET    /posts/:id.:format               {:action=>"show", :controller=>"posts"}
                     PUT    /posts/:id                       {:action=>"update", :controller=>"posts"}
                     PUT    /posts/:id.:format               {:action=>"update", :controller=>"posts"}
                     DELETE /posts/:id                       {:action=>"destroy", :controller=>"posts"}
                     DELETE /posts/:id.:format               {:action=>"destroy", :controller=>"posts"}

情況2的輸出:

           news_index GET    /news                            {:action=>"index", :controller=>"posts"}
 formatted_news_index GET    /news.:format                    {:action=>"index", :controller=>"posts"}
                      POST   /news                            {:action=>"create", :controller=>"posts"}
                      POST   /news.:format                    {:action=>"create", :controller=>"posts"}
             new_news GET    /news/new                        {:action=>"new", :controller=>"posts"}
   formatted_new_news GET    /news/new.:format                {:action=>"new", :controller=>"posts"}
            edit_news GET    /news/:id/edit                   {:action=>"edit", :controller=>"posts"}
  formatted_edit_news GET    /news/:id/edit.:format           {:action=>"edit", :controller=>"posts"}
                 news GET    /news/:id                        {:action=>"show", :controller=>"posts"}
       formatted_news GET    /news/:id.:format                {:action=>"show", :controller=>"posts"}
                      PUT    /news/:id                        {:action=>"update", :controller=>"posts"}
                      PUT    /news/:id.:format                {:action=>"update", :controller=>"posts"}
                      DELETE /news/:id                        {:action=>"destroy", :controller=>"posts"}
                      DELETE /news/:id.:format                {:action=>"destroy", :controller=>"posts"}

我在情況2中有錯誤,因為在我的源代碼中我沒有edit_news ,例如,我有<%= link_to 'Edit', edit_post_path(post) %> edit_news <%= link_to 'Edit', edit_post_path(post) %>

“新聞”一詞在單數(成員)和復數(集合)名稱之間有歧義。 您可以嘗試解決此問題,但最終可能會使自己和rails感到困惑。 (“ news_path”應為該成員使用id參數,還是收集路徑?什么是“新聞”?)

讓我們堅持稱其為帖子:

map.resources :posts, :as => "news", :singular => "news"
  • 您的路線將是“ /新聞”
  • 您的控制器將是PostsController
  • 您的路徑助手將是posts_path,post_path,edit_post_path等。

換句話說,在所有情況下,您都將資源稱為“帖子”,但它們將顯示在“ / news / *”下的路由中。

您的第一步很好:將所有Post *類替換為News *類。 調用模型News和控制器NewsController應該不會有問題。 確保在代碼中找到所有匹配項(也應將news_path類的post_path替換為news_path )。

接下來,將您的路線修改為

map.resources :news

為了使用NewsController而不是PostsController。 它應該工作。

注意 :不要忘記重新啟動Web服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM