2015-10-13 125 views
1

我一直在试图搭建一个用户的基本网站博客。我有想将联系人页面加入数据库,在那里你可以操纵里面的链接。并编辑它们等,但都在同一页上,避免创建edit.html.erbshow.html.erb。只是索引和_form,我跺脚。顺便说一句,这是我尝试用任何语言开发的第一个网站。Rails缺少必需的键:[:id]

这是index.html.erb,不断失败我。

<div class="indent50"> 
    <h3>For any questions contact me</h3> 
    <% @contacts.each do |contact| %> 
     <h5><%= link_to contact.name, "http://#{contact.clink}" %></h5> 
     <h5><%= link_to "Edit",edit_contact_path(@contact) %></h5> 
    <% end %> 
</div> 
<%= render "form" %> 

提高No route matches {:action=>"edit", :controller=>"contacts", :id=>nil} missing required keys: [:id]这里<h5><%= link_to "Edit",edit_contact_path(@contact) %></h5>

这是接触控制:

class ContactsController < ApplicationController 
    before_action :find_contact, only: [:show, :edit, :update, :destroy] 
    def index 
     @contacts = Contact.all.order("created_at DESC") 
    end 
    def new 
     @contact = Contact.new 
    end 

    def create 
     @contact = Contact.new(post_params) 
     if @contact.save 
      flash[:notice] = "Contact created" 
      redirect_to(:action=>'index', :contact_id => @contact.id) 
     else 
      @contacts = Contacts.order() 
      render('new') 
     end 
    end 

    def edit 
    end 

    private 
    def find_contact 
     @contact=Contact.find(params[:id]) 
    end 
    def post_params 
     params.require(:contact).permit(:name, :clink) 
    end 
end 

有什么建议?或者甚至是我想到的替代方案。谢谢。

+0

请显示'rake routes'。 – Smar

+2

'routes.rb'应该有'resources:contact'和'edit_contact_path(@contact)'应该是'edit_contact_path(contact)' –

回答

4

您需要用您的link_to "Edit"产品系列中的@contact替换为contact

@contact不存在,您需要使用您创建的循环变量,名为contact。这就是为什么它说“缺少必需的密钥”,因为您发送的@contact具有价值nil

+0

圣洁......那真是愚蠢。非常感谢 !!! –

+0

欢迎来到Stackoverflow:p – Caillou

相关问题