2016-07-07 54 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