代码之家  ›  专栏  ›  技术社区  ›  Charles Smith

Rails 5嵌套资源URL具有多个直通关联

  •  0
  • Charles Smith  · 技术社区  · 8 年前

    我已经创建了一个小的测试应用程序,利用有很多通过关联的方法,但我似乎可以得到 link_to

    错误消息

    No route matches {:action=>"show", :controller=>"categories", :location_id=>#<Category id: 1, name: "parties", created_at: "2017-11-01 17:40:25", updated_at: "2017-11-01 17:40:25">}, missing required keys: [:id]
    

    class Location < ApplicationRecord
      belongs_to :product
      belongs_to :category
    end
    

    类别rb型

    class Category < ApplicationRecord
      has_many :locations
      has_many :products, through: :locations
    end
    

    产品rb型

    class Product < ApplicationRecord
      has_many :locations
      has_many :categories, through: :locations
    end
    

    路线。rb型

    Rails.application.routes.draw do
      resources :locations do
        resources :categories do
          resources :products
        end
      end
      root :to => 'locations#index'
    end
    

    类别/索引。html

    ...
    <% @categories.each do |category| %>
       <tr>
         <td><%= category.name %></td>
         <td><%= link_to 'Show', location_category_path(category) %></td>
         <td><%= link_to 'Edit', edit_location_category_path(category) %></td>
         <td><%= link_to 'Destroy', location_categories_path(category), method: :delete, data: { confirm: 'Are you sure?' } %></td>
       </tr>
    <% end %>
    ...
    

    127.0.0.1:3000/轨道/信息/路线

    location_category_products_path GET /locations/:location_id/categories/:category_id/products(.:format)  
    products#index
    
    POST    /locations/:location_id/categories/:category_id/products(.:format)  
    products#create
    
    new_location_category_product_path  GET /locations/:location_id/categories/:category_id/products/new(.:format)  
    products#new
    
    edit_location_category_product_path GET /locations/:location_id/categories/:category_id/products/:id/edit(.:format) 
    products#edit
    
    location_category_product_path  GET /locations/:location_id/categories/:category_id/products/:id(.:format)  
    products#show
    
    PATCH   /locations/:location_id/categories/:category_id/products/:id(.:format)  
    products#update
    
    PUT /locations/:location_id/categories/:category_id/products/:id(.:format)  
    products#update
    
    DELETE  /locations/:location_id/categories/:category_id/products/:id(.:format)  
    products#destroy
    
    location_categories_path    GET /locations/:location_id/categories(.:format)    
    categories#index
    
    POST    /locations/:location_id/categories(.:format)    
    categories#create
    
    new_location_category_path  GET /locations/:location_id/categories/new(.:format)    
    categories#new
    
    edit_location_category_path GET /locations/:location_id/categories/:id/edit(.:format)   
    categories#edit
    
    location_category_path  GET /locations/:location_id/categories/:id(.:format)    
    categories#show
    
    PATCH   /locations/:location_id/categories/:id(.:format)    
    categories#update
    
    PUT /locations/:location_id/categories/:id(.:format)    
    categories#update
    
    DELETE  /locations/:location_id/categories/:id(.:format)    
    categories#destroy
    
    locations_path  GET /locations(.:format)    
    locations#index
    
    POST    /locations(.:format)    
    locations#create
    
    new_location_path   GET /locations/new(.:format)    
    locations#new
    
    edit_location_path  GET /locations/:id/edit(.:format)   
    locations#edit
    
    location_path   GET /locations/:id(.:format)    
    locations#show
    
    PATCH   /locations/:id(.:format)    
    locations#update
    
    PUT /locations/:id(.:format)    
    locations#update
    
    DELETE  /locations/:id(.:format)    
    locations#destroy
    
    root_path   GET /   
    locations#index
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Cyzanfar    8 年前

    对于路线:

    location_category_path  GET /locations/:location_id/categories/:id(.:format)    
    

    <!-- /locations/:location_id/categories/:id -->
    <%= link_to 'Show', [{location_id}, {category_id}] %>
    
        2
  •  1
  •   Arslan Ali    8 年前
    location_category_path  GET /locations/:location_id/categories/:id(.:format)    
    

    如您所见,路线: location_category_path 需要两件事: :location_id & :id (the id 您要引用的类别)。

    在您的例子中,您只指定了一个,而不是另一个。您还需要指定 :location\u id .

    还有一种较短的方法可以编写此URL: [location_id, category_id] . 只需编写一个以两个ID开头的数组 location_id .

        3
  •  -1
  •   Jay-Ar Polidario    8 年前

    再加上阿尔斯兰的回答。

    您可能需要设置 @location 在控制器中,如果您还没有。

    应用程序/控制器/类别_控制器。rb型

    class CategoriesController < ApplicationController
      def index
        @location = # your code here
      end
    end
    

    应用程序/视图/类别/索引。html。erb公司

    <% @categories.each do |category| %>
       <tr>
         <td><%= category.name %></td>
         <td><%= link_to 'Show', location_category_path(@location.id, category.id) %></td>
         <td><%= link_to 'Edit', edit_location_category_path(@location.id, category.id) %></td>
         <td><%= link_to 'Destroy', location_categories_path(@location.id, category.id), method: :delete, data: { confirm: 'Are you sure?' } %></td>
       </tr>
    <% end %>
    

    对于路由URL参数,您可以使用:

    location_category_path(@location.id, category.id)
    

    :location_id 然后 :id )当你这么做的时候 bundle exec rake routes ,也可以具体如下:

    location_category_path(location_id: @location.id, id: category.id)