Ruby 在 Rails - 创建一个自定义路由到 #Show

Ruby on Rails - Creating a Custom Route to #Show

我仍在制作我的食谱应用程序,我正在构建杂货清单功能,您可以在其中单击 link "Add to Grocery List",它将添加所有从给定食谱到新页面上的杂货清单的成分,您可以在杂货店时在智能手机上拉起该页面,并在您去的时候标记项目。

我在获得正确的路线时遇到了一些困难。

所以我已经在 grocery#show 页面上显示了我想要的购物清单。我到了我想将 link 添加到食谱页面的地步,以便访问者可以简单地单击 link,他们将被移至杂货清单。

现在为了进入食谱 #2 的购物清单页面,您必须使用以下内容 url:

localhost:3000/recipes/2/groceries/1 

目前如您所见,我们必须使用以下路线:

recipe_grocery GET    /recipes/:recipe_id/groceries/:id(.:format) groceries#show

这是我想要的:

localhost:3000/recipes/2/groceries

我敢肯定这是一件很容易做到的事情,但此刻我却忘记了。如果您能提供任何帮助,我将永远感激不已!

这是我的佣金路线的下半部分目前的样子:

     recipe_favorites POST   /recipes/:recipe_id/favorites(.:format)     favorites#create
     recipe_favorite DELETE /recipes/:recipe_id/favorites/:id(.:format) favorites#destroy
    recipe_groceries POST   /recipes/:recipe_id/groceries(.:format)     groceries#create
      recipe_grocery GET    /recipes/:recipe_id/groceries/:id(.:format) groceries#show
             recipes GET    /recipes(.:format)                          recipes#index
                     POST   /recipes(.:format)                          recipes#create
          new_recipe GET    /recipes/new(.:format)                      recipes#new
         edit_recipe GET    /recipes/:id/edit(.:format)                 recipes#edit
              recipe GET    /recipes/:id(.:format)                      recipes#show
                     PATCH  /recipes/:id(.:format)                      recipes#update
                     PUT    /recipes/:id(.:format)                      recipes#update
                     DELETE /recipes/:id(.:format)                      recipes#destroy
              drinks GET    /drinks(.:format)                           recipes#index {:category=>"drinks"}
             entrees GET    /entrees(.:format)                          recipes#index {:category=>"entrees"}
               sides GET    /sides(.:format)                            recipes#index {:category=>"sides"}
            desserts GET    /desserts(.:format)                         recipes#index {:category=>"desserts"}
          appetizers GET    /appetizers(.:format)                       recipes#index {:category=>"appetizers"}
               about GET    /about(.:format)                            welcome#about
                    root GET    /                                       welcome#index

这是我的路线文件:

Rails.application.routes.draw do

  devise_for :users
  resources :users, only: :show
  get 'comments/index'

  get 'comments/create'

  get 'comments/show'

  get 'comments/edit'

  get 'comments/new'

  resources :recipes do
    resources :comments, only: [:create, :show, :index, :new, :destroy]
    resources :favorites, only: [:create, :destroy]
    resources :groceries, only: [:create, :destory, :show]
  end

  get 'drinks' => 'recipes#index', :category => "drinks"
  get 'entrees' => 'recipes#index', :category => "entrees"
  get 'sides' => 'recipes#index', :category => "sides"
  get 'desserts' => 'recipes#index', :category => "desserts"
  get 'appetizers' => 'recipes#index', :category => "appetizers"


  get 'about' => 'welcome#about'

  root to: 'welcome#index'

end

要获得这样的路线:

localhost:3000/recipes/2/groceries

您可以更新您的 routes.rb 文件:

resources :recipes do
  # other routes
  resources :groceries, only: [:create, :destory, :show, :index] # adding :index to the list
end

这个,会给你一条新路线:

recipe_groceries GET    /recipes/:recipe_id/groceries(.:format)     groceries#index

然后,您将获得上述路线,该路线将列出特定 recipe 的所有 groceries,在您的上述情况下,recipeid 2。