I am still making my cookbook app, Im to the point to where I am building out the grocery list feature where you will be able to click a link "Add to Grocery List" and it will add all the ingredients from a given recipe to a grocery list on a new page, where you will be able to pull the page up on your smartphone while at the grocery store and mark off items as you go.
I am having some difficulty though with getting the correct route.
So I've got the grocery list appearing on the grocery#show page which is where I want it. I am to the point where I want to add the link to the recipe page so that a visitor can simple click the link, and they will be move to the grocery list.
Right now in order to get to the grocery list page for recipe #2 you must use the following url:
localhost:3000/recipes/2/groceries/1
Currently as you can see we must use the following route:
recipe_grocery GET /recipes/:recipe_id/groceries/:id(.:format) groceries#show
Heres what I want it to be instead:
localhost:3000/recipes/2/groceries
I'm sure it quite an easy thing to do, but at the moment its escaped me. So any help you could provide, I would be forever grateful!
Here is what the bottom half of my rake routes currently looks like:
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
Here is my route file:
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
To get a route like this:
localhost:3000/recipes/2/groceries
You can update your routes.rb
file with this:
resources :recipes do
# other routes
resources :groceries, only: [:create, :destory, :show, :index] # adding :index to the list
end
This, will give you a new route:
recipe_groceries GET /recipes/:recipe_id/groceries(.:format) groceries#index
Then, you will have the above route that will list all the groceries
for a particular recipe
, in your above case the recipe
with id
2.