2016-01-17 73 views
0

我試圖構建的是嵌套在一個用戶和shopmain路線後面的對象鏈接:用戶> shompmain> shopaddress編輯的link_to

我不能讓顯示/編輯/刪除鏈接工作。

<%= link_to 'Show', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %> 
<%= link_to 'Edit', edit_user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %> 
<%= link_to 'Delete', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-danger btn-sm" %> 

我得到這個讀取類似的錯誤。這似乎是因爲某些原因我錯過了店鋪地址的ID。

No route matches {:action=>"show", :controller=>"shop/shopaddresses", :id=>nil, :shopmain_id=>#<Shop::Shopaddress id: 2, shopadrsline1: "Civic Place", shopadrsline2: "Box 888", shopadrscity: "Toronto", shopadrsprovince: "ON", shopadrspostalcode: "N0N1V0", shopmain_id: 2, created_at: "2016-01-17 03:38:47", updated_at: "2016-01-17 03:38:47">, :user_id=>#<Shop::Shopmain id: 2, shopname: "Eastwood", shoptagline: "Testing", user_id: 1, created_at: "2016-01-17 03:30:01", updated_at: "2016-01-17 03:30:01">} missing required keys: [:id] 

我的路線文件readslike

Rails.application.routes.draw do 

    # Devise Authetication 
    devise_for :users 
    devise_for :admins 

    # Website Pages 
    root 'website/page#index' 

    # Backend Shop Module 
    resources :users do 
     scope module: "shop" do 
      resources :shopmains do 
       resources :shopdetails 
       resources :shopaddresses 
       resources :shophours 
      end 
     end 
    end 

end 

我shopaddresses控制器看起來像:

class Shop::ShopaddressesController < ApplicationController 

    # Security and Layouts 
    before_filter :authenticate_user! 
    layout 'layouts/shop/application.html.erb' 
    before_action :set_shopaddress, only: [:show, :edit, :update, :destroy] 

    # Shop Address Index 
    def index 
     shopmain = Shop::Shopmain.find(params[:shopmain_id]) 
     @shopaddresses = shopmain.shopaddresses 
    end 

    # Shop Address Single View 
    def show 
     shopmain = Shop::Shopmain.find(params[:shopmain_id]) 
     @shopmain = shopmain.shopaddresses.find(params[:id]) 
    end 

    # New Shop Address 
    def new 
     shopmain = Shop::Shopmain.find(params[:shopmain_id]) 
     @shopaddress = shopmain.shopaddresses.build 
     respond_to do |format| 
      format.html # new.html.erb 
      format.xml { render :xml => @shopmain } 
     end 
    end 

    # Edit Shop Address 
    def edit 
     shopmain = Shop::Shopmain.find(params[:shopmain_id]) 
     @shopaddress = shopmain.shopaddresses.find(params[:id]) 
    end 

    # Create a New Shop Address 
    def create 
     @shopmain = Shop::Shopmain.find(params[:shopmain_id]) 
     @shopaddress = @shopmain.shopaddresses.create(shopaddress_params) 
     respond_to do |format| 
     if @shopaddress.save 
      format.html { redirect_to user_shopmain_shopaddresses_path, notice: 'Comment for Shopmain was Successfully Created.' } 
      format.json { render action: 'show', status: :created, location: @shopaddress } 
      else 
       format.html { render action: 'new' } 
       format.json { render json: @shopaddress.errors, status: :unprocessable_entity } 
      end 
     end 
    end 

    # Update Shop Address 
    def update 
     respond_to do |format| 
     if @shopaddress.update(shopaddress_params) 
      format.html { redirect_to user_shopmain_shopaddresses_path, notice: 'Comment for Shopmain was Successfully Updated.' } 
      format.json { head :no_content } 
      else 
       format.html { render action: 'edit' } 
       format.json { render json: @shopaddress.errors, status: :unprocessable_entity } 
      end 
     end 
    end 

    # Delete Shop Address 
    def destroy 
     @shopmain = Shop::Shopmain.find(params[:shopmain_id]) 
     @shopaddress = @shopmain.shopaddresses.find(params[:id]) 
     @shopaddress.destroy 
     respond_to do |format| 
      format.html { redirect_to user_shopmain_shopaddresses_path, notice: 'Comment for Shopmain was Successfully Deleted.' } 
      format.json { head :no_content } 
     end 
    end 

    private 

    # Common Constrainsts 
    def set_shopaddress 
     @shopaddress = Shop::Shopaddress.find(params[:id]) 
    end 

    # Strong Params Whitelist 
    def shopaddress_params 
     params.require(:shop_shopaddress).permit(:shopadrsline1, :shopadrsline2, :shopadrscity, :shopadrsprovince, :shopadrspostalcode, :shopmain_id) 
    end 

end 

完整的索引視圖(每個評論的請求)

<!-- Information Card --> 
<p id="notice"><%= notice %></p> 
<div class="wrapper wrapper-content"> 
    <div class="panel"> 
     <div class="panel-heading"> 
      <h4 class="panel-title">Shop Listing Index</h4> 
     </div> 
     <div class="panel-body"> 

      <table class="table table-bordered table-striped table-hover"> 

       <thead> 
        <tr> 
         <th>Address Line 1</th> 
         <th>Address Line 2</th> 
         <th>City</th> 
         <th>Province</th> 
         <th>Code</th> 
         <th></th> 
        </tr> 
       </thead> 

       <tbody> 
        <% @shopaddresses.each do |shopaddress| %> 
         <tr> 
          <td><%= shopaddress.shopadrsline1 %></td> 
          <td><%= shopaddress.shopadrsline2 %></td> 
          <td><%= shopaddress.shopadrscity %></td> 
          <td><%= shopaddress.shopadrsprovince %></td> 
          <td><%= shopaddress.shopadrspostalcode %></td> 
          <td> 
           <div class="btn-group" role="group" aria-label="Member Contact Group"> 
            <%= link_to 'Show', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %> 
            <%= link_to 'Edit', edit_user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %> 
            <%= link_to 'Delete', user_shopmain_shopaddress_path(shopaddress.shopmain, shopaddress), method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-danger btn-sm" %> 
           </div> 
          </td> 
         </tr> 
        <% end %> 
       </tbody> 

      </table> 

      <%= link_to 'New Address', new_user_shopmain_shopaddress_path, :class => "btn btn-primary btn-sm" %> 

     </div> 
    </div> 
</div> 
+0

範你更新問題與查看頁面的完整代碼? – Pavan

+0

謝謝帕文。在底部插入完整視圖。 – bradpotts

+0

我剛剛意識到一些代碼丟失,只是修復它。 – bradpotts

回答

1

我驚訝你說你的節目鏈接工作因爲它顯然無法工作。 :)你的路線助手期望三個參數,而不是兩個 - 你缺少用戶ID。所以它應該是:

<%= link_to 'Show', user_shopmain_shopaddresses_path(current_user, shopaddress.shopmain, shopaddress), :class => "btn btn-primary btn-sm" %> 
+0

謝謝我的壞,我應該更新我的問題,當顯示鏈接開始失敗。令人驚訝的是,通過改變路線爲複數(等等)使節目鏈接工作,但其他人從未做過。 但無論如何感謝您的回答,這有助於和工作:) – bradpotts