2015-10-13 68 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

相關問題