2017-11-03 8 views
0

だから私はこれを何時間もやってきました。私は関連を持つhas-manyを持つアプリを持っており、親リソースのidを子に渡そうとしています。ルーティングは機能しますが、私は周囲をブラウズしたり、新しいオブジェクトを作成したりすることもできますが、URLによってパスにIDが表示されません。親リソースのIDをRailsの子リソースに渡す5

期待&望ましい結果

http://127.0.0.1:3000/locations/1/groups/1/products/1 

現在の結果

http://127.0.0.1:3000/locations/location/groups/group/products/1 

routes.rbを

... 
resources :locations do 
resources :groups do 
    resources :products 
end 
end 
... 

products_controller.rb 私のすべてのコントローラは、このコントローラと同様にセットアップされています。

class ProductsController < ApplicationController 
    before_action :set_product, only: [:show, :edit, :update, :destroy] 

    def index 
    @locations = Location.all 
    @products = Product.all 
    @groups = Group.all 
    end 

    def show 
    @product = Product.find(params[:id]) 
    end 

    def new 
    @product = Product.new 
    end 

    def edit 
    @product = Product.find(params[:id]) 
    end 

    def create 
    @product = Product.new(product_params) 

    respond_to do |format| 
     if @product.save 
     format.html { redirect_to location_group_products_path(@product), notice: 'Product was successfully created.' } 
     format.json { render :show, status: :created, location: @product } 
     else 
     format.html { render :new } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @product.update(product_params) 
     format.html { redirect_to location_group_product_path(@product), notice: 'Product was successfully updated.' } 
     format.json { render :show, status: :ok, location: @product } 
     else 
     format.html { render :edit } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @product.destroy 
    respond_to do |format| 
     format.html { redirect_to location_group_products_path(@product), notice: 'Product was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 

    def set_product 
     @product = Product.find(params[:id]) 
    end 

    def product_params 
     params.require(:product).permit(:name, :location_id, :group_id, :product_id) 
    end 
end 

products.html.rb

<h1>Products</h1> 

<table> 
    <thead> 
    <tr> 
     <th>Name</th> 
     <th colspan="3"></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @products.each do |product| %> 
     <tr> 
     <td><%= product.name %></td> 
     <td><%= link_to 'Show', location_group_product_path(:location, :group, product) %></td> 
     <td><%= link_to 'Edit', edit_location_group_product_path(:location, :group, product.id) %></td> 
     <td><%= link_to 'Destroy', location_group_product_path(:location, :group, product.id), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

<%= link_to 'New Product', new_location_group_product_path %> 

答えて

0

あなたは場所やグループだけでなく、彼らのシンボルのIDS(またはオブジェクト)を渡す必要があります。このようなもの:

location_group_product_path(product.group.location, product.group, product) 
edit_location_group_product_path(product.group.location, product.group, product) 
+0

ありがとうございます。私は最初に位置idを渡す必要があり、あなたの例では最初のパスは後方にあります。私はそれを試して、 '#エラーの未定義メソッドグループを受け取りました。 –

関連する問題