Rails URL生成错误但路由存在

时间:2016-07-07 02:04:15

标签: ruby-on-rails-4

创建一个基本的电话簿应用程序并进行一些更改,同时找出has_many和belongs_to关系。我必须打破一些东西,因为我不知道为什么我会收到这个错误。当我访问我的root时,我得到以下内容 - >

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'

这是我的contacts / 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

这是我的联系人/ 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

正如你所看到的,我有一个联系方式#show,这不是错误。我不清楚它可能是什么。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好像你遗漏了:id中的contact_path

contacts/index.html.erb中,更改此内容:

<%= link_to contact.name, contact_path %> 

到此:

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