2016-07-07 59 views
1

製作一個基本的電話簿應用程序,並找出has_many和belongs_to關係進行了一些更改。我一定是因爲不知道爲什麼會出現這個錯誤而弄壞了某些東西。當我進入我的根,我得到以下 - >Rails URL生成錯誤,但路由存在

ActionController::UrlGenerationError in ContactsController#index 
No route matches {:action=>"show", :controller=>"contacts"} missing required keys: [:id] 

錯誤顯示行錯誤:

app/views/contacts/index.html.erb:10:in `block in _app_views_contacts_index_html_erb___2771775118522806317_70170309989460' 
app/views/contacts/index.html.erb:7:in `_app_views_contacts_index_html_erb___2771775118522806317_70170309989460' 

這是我的聯繫人/ index.html.erb

<p id="notice"><%= notice %></p> 

<% if user_signed_in? %> 

    <h1>Listing Contacts</h1> 
    <% @contacts = current_user.contacts %> 
     <% @contacts.each do |contact| %> 
     <div class="link row clearfix"> 
      <h2> 
      <%= link_to contact.name, contact_path %> 
      </h2> 
     </div> 
     <% end %> 
    <% end %> 
    <%= link_to "New Contact", new_contact_path %> 

<% else %> 
    <h5> Welcome. Make an account or sign in above! </h5> 
<% end %> 

這是我的配置/路線

Rails.application.routes.draw do 
    resources :controllers 
    devise_for :users 

    resources :contacts so 
    resources :numbers 
    end 
end 
end 

This是我的聯繫人/ show.html.erb

<div class="page-header"> 
    <h1><a href="<%= @contact.name %>"><%= @contact.name %></a><br> </h1> 
</div> 

<p> 
    <strong>Name:</strong> 
    <%= @contact.name %> 
</p> 

<p> 
    <strong>Email:</strong> 
    <%= @contact.email %> 
</p> 

<br> 
<%= link_to 'Edit', edit_contact_path(@contact) %> | 
<%= link_to 'Back', contacts_path %> 

我耙路線的輸出:

contacts GET /contacts(.:format)    contacts#index 
         POST /contacts(.:format)    contacts#create 
      new_contact GET /contacts/new(.:format)   contacts#new 
      edit_contact GET /contacts/:id/edit(.:format) contacts#edit 
       contact GET /contacts/:id(.:format)   contacts#show 
         PATCH /contacts/:id(.:format)   contacts#update 
         PUT /contacts/:id(.:format)   contacts#update 
         DELETE /contacts/:id(.:format)   contacts#destroy 

正如你所看到的,我有接觸#顯示路由所以這不是錯誤。我不清楚它可能是什麼。有任何想法嗎?

+0

什麼路線,你正在使用什麼呢?它也可能有助於將代碼發佈到聯繫人控制器中。 –

+0

根路由是contacts#index。 – Sunny

+0

抱歉,我的意思是你正在使用的網址是什麼,比如localhost:3000/contacts /? etc. –

回答

0

似乎你缺少:idcontact_path

contacts/index.html.erb,改變這種:

<%= link_to contact.name, contact_path %> 

這樣:

<%= link_to contact.name, contact_path(contact.id) %> 
+1

任何想法爲什麼它更早工作,但現在需要輸入的ID?只要網站允許我將您的答案標記爲正確。 – Sunny

+0

我不知道爲什麼,除非你改變了'config/routes.rb'中的任何東西。考慮使用* git *並定期提交您的代碼 - 然後您可以使用'git diff'比較提交:https://git-scm.com/docs/git-diff – SoAwesomeMan