我还在制作我的食谱应用程序,我到了建立购物清单功能的地方,你可以点击链接“添加到杂货店列表”,它将添加给定的所有成分在新页面上购买食品清单的配方,您可以在杂货店的同时在智能手机上提取页面并随时标记项目。
虽然获得正确的路线但我遇到了一些困难。
所以我的杂货店名单出现在杂货店#show页面上,这就是我想要的地方。我想要将链接添加到配方页面,以便访问者可以简单地点击链接,然后他们将移动到购物清单。
现在,为了进入食谱#2的购物清单页面,您必须使用以下网址:
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
答案 0 :(得分:2)
要获得这样的路线:
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
然后,您将拥有上述路线,该路线将列出特定groceries
的所有recipe
,在您的上述情况下,recipe
与id
2。