我一直在試圖搭建一個用戶的基本網站博客。我有想將聯繫人頁面加入數據庫,在那裏你可以操縱裏面的鏈接。並編輯它們等,但都在同一頁上,避免創建edit.html.erb
或show.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
有什麼建議?或者甚至是我想到的替代方案。謝謝。
請顯示'rake routes'。 – Smar
'routes.rb'應該有'resources:contact'和'edit_contact_path(@contact)'應該是'edit_contact_path(contact)' –