【问题标题】:Ruby on Rails - Creating a Custom Route to #ShowRuby on Rails - 创建自定义路由到#Show
【发布时间】:2015-10-21 19:00:55
【问题描述】:

我仍在制作我的食谱应用程序,我正在构建杂货清单功能,您可以在其中单击“添加到杂货清单”链接,它将添加给定的所有成分将食谱添加到新页面上的杂货清单中,您可以在杂货店时在智能手机上拉出该页面并随时标记商品。

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

所以我的杂货清单出现在了我想要的杂货#show 页面上。我想将链接添加到食谱页面,以便访问者可以简单地单击链接,他们将被移动到购物清单。

现在要进入食谱 #2 的购物清单页面,您必须使用以下网址:

localhost:3000/recipes/2/groceries/1 

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

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

这就是我想要的:

localhost:3000/recipes/2/groceries

我确信这是一件很容易的事情,但现在它逃过了我的视线。因此,您可以提供任何帮助,我将永远感激不尽!

这是我的 rake 路由的下半部分目前的样子:

     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

【问题讨论】:

    标签: html ruby-on-rails ruby custom-routes


    【解决方案1】:

    要获得这样的路线:

    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。

    【讨论】:

    • 我不敢相信我没有意识到这一点,谢谢先生!效果很好!
    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多