2016-11-22 48 views
0

我正在第一次學習Ruby on Rails,並且一直在嘗試設置我的筆記本電腦以繼續與我的項目在桌面上的位置。我昨天在運行rvm的rvm和桌面上運行Ruby的筆記本電腦出現了一些問題,所以我解決了這個問題。但是,現在我的筆記本電腦出現以下錯誤,但筆記本電腦上的所有內容都正常工作。UrlGenerationError在一臺計算機上沒有路由匹配,但在另一臺計算機上沒有路由匹配

ActionController::UrlGenerationError in Categories#index 

No route matches {:action=>"show", :controller=>"categories", :id=>nil} missing required keys: [:id] 

     <table> 
     <tr> 
      <th class="table-header"><%= link_to 'housing', category_path(@housing) %></th> 
     </tr> 
     <tr> 
      <td> 

我相信它應該工作。如果我明白代碼正確,我通過傳入@housing參數,通過類別路徑鏈接到我的住房類別。

就像我說的,這可以在我的桌面上運行。

代碼如下感謝提前任何幫助。從我的耙路線

輸出命令我想使用的路線......

category GET /categories/:id(.:format) categories#show 

index.html.erb

<div class="container"> 
    <div class="leftbar"> 
    <a href="#"><h1>rubyslist</h1></a> 
    <ul> 
     <a href="#"><%= link_to 'post a classifieds', new_listing_path %></a> 
     <li>my account</li> 
    </ul> 
     <p>search shiplist</p> 
     <%= form_tag search_listings_path, :method => 'get' do %> 
     <p> 
     <%= text_field_tag :search, params[:search] %> 
     <%= submit_tag "Search", :name => nil %> 
     </p> 
     <% end %> 
    </div> 
    <div class="center-column" > 
    <div class="center-one"> 
     <table> 
     <tr> 
      <th class="table-header"><%= link_to 'housing', category_path(@housing) %></th> 
     </tr> 
     <tr> 
      <td> 
      <% @housing.subcategories.each do |subcategory| %> 
      <ul class="left-list"> 
       <li><%= link_to subcategory.name, category_subcategory_path(@housing, subcategory) %></li> 
      </ul> 
      <% end %> 
      </td> 
     </tr> 
     </table> 
     <table> 
     <tr> 
      <th class="table-header"><%= link_to 'roommates', category_path(@roommates) %></th> 
     </tr> 
     <tr> 
      <td> 
      <% @roommates.subcategories.each do |subcategory| %> 
      <ul> 
       <li><%= link_to subcategory.name, category_subcategory_path(@roommates, subcategory) %></li> 
      </ul> 
      <% end %> 
      </td> 
     </tr> 
     </table> 
     <table> 
     <tr> 
      <th class="table-header"><%= link_to 'for sale', category_path(@forsale) %></th> 
     </tr> 
     <tr> 
      <td> 
      <% @forsale.subcategories.each do |subcategory| %> 
      <ul class="left-list"> 
       <li><%= link_to subcategory.name, category_subcategory_path(@forsale, subcategory) %></li> 
      </ul> 
      <% end %> 
      </td> 
     </tr> 
     </table> 
    </div> 
    </div> 
    <div class="rightbar"> 
    <!-- Links to the pages via routes match --> 
    <ul class="rightbar-top"> 
     <!-- /help --> 
     <li><%= link_to 'help and faq', help_path%></li> 
     <li><%= link_to 'avoid scams and fraud', scams_path%></li> 
     <li><%= link_to 'personal safety tips', safety_path%></li> 
     <li class="rightbar-top"><%= link_to 'terms of use', terms_path%></li> 
     <li><%= link_to 'privacy policy', privacy_path%></li> 
     <li><%= link_to 'about shiplist', about_path%></li> 
     <li><%= link_to 'contact us', contact_path%></li> 
    </ul> 
    </div> 
</div> 

的routes.rb

#Creates CRUD actions for categories 
    resources :categories do 
    #Append sub-categories to categories with CRUD 
    resources :subcategories 
    end 

    #Performs the search 
    resources :listings do 
    collection do 
     get 'search' 
    end 
    end 

    #Set homepage to index of categories 
    root 'categories#index' 

    #Matches the pages help action to a get request 
    #for the help page with page controller in help 
    #function of pages controller 
    match '/help', to: 'pages#help', via: :get 
    match '/scams', to: 'pages#scams', via: :get 
    match '/safety', to: 'pages#safety', via: :get 
    match '/about', to: 'pages#about', via: :get 
    match '/contact', to: 'pages#contact', via: :get 
    match '/privacy', to: 'pages#privacy', via: :get 
    match '/terms', to: 'pages#terms', via: :get 
    match '/subcategories/find_by_category', to: 'subcategories#find_by_category', via: :post 
end 

categories_controller.rb

class CategoriesController < ApplicationController 
    #The index page of our categories controller 
    def index 
     @categories = Category.all 
     @housing = @categories[0] 
     @roommates = @categories[1] 
     @forsale = @categories[2] 
    end 

    def show 
     @listings = Listing.where(category_id: params[:id]) 
    end 
end 

回答

0

運行解決了這一問題的命令

rake db:seed 

相關問題