我正在爲通訊錄創建基本應用程序,並希望將聯繫人與用戶相關聯。將聯繫人與用戶相關聯
我已經爲用戶設置了身份驗證,現在正在嘗試將我的聯繫人表與用戶關聯起來。我有兩個獨立的控制器,一個用於用戶,另一個用於聯繫人,然後我的計劃是通過belongs_to:user和has_many:contacts將它們關聯起來。
我完全被錯誤難倒我不斷收到雖然
undefined method `new' for Contact:Module
據我所知,我在接觸控制器正確定義新的,但由於某種原因,我不能得到這個工作。
我有以下的代碼,此刻
路線:
Contact::Application.routes.draw do
get "sessions/new"
get "logout" => "sessions#destroy"
controller :user do
get "signup" => "user#new"
end
resources :users, :controller => 'user'
controller :sessions do
get "login" => "sessions#new"
post "login" => "sessions#create"
delete "logout" => "sessions#destroy"
end
controller :dashboard do
get "home" => "dashboard#home"
end
controller :contact do
get "newcontact" => "contact#new"
end
resources :contacts, :controller => 'contact'
root :to => 'sessions#new'
end
聯繫控制器
class ContactController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new (params[:contact])
if @contact.save
redirect_to root_url
else
render "contact#new"
end
end
end
聯繫/新形式
<%= form_for @contact do |f| %>
<% if @contact.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @contact.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class = "name-field">
<%= f.text_field :name, :placeholder => "Name" %>
</div>
<div class = "company-field">
<%= f.text_field :company, :placeholder => "Company" %>
</div>
<div class = "email-field">
<%= f.text_field :email, :placeholder => "Email" %>
</div>
<div class = "phone-field">
<%= f.text_field :phone, :placeholder => "Phone" %>
</div>
<div class = "mobile-field">
<%= f.text_field :mobile, :placeholder => "Mobile" %>
</div>
<div class="actions"><%= f.submit "Add Contact" %></div>
</div>
<% end %>
任何幫助的人能提供至修復這個錯誤真的會非常感激,因爲我完全卡住了!我認爲這可能與聯繫一詞的多元化有關,但似乎找不到解決問題的正確方法。
非常感謝您的幫助! Tom
類的ContactController
Mishaux
2012-04-04 00:30:18
您可以爲錯誤的回溯? – Ekampp 2012-04-05 07:22:12
@Mishaux - 我試着改變ContactController到ContactsController,而是接收'期望的C:/Sites/contact/app/controllers/contact_controller.rb來定義ContactController'。 – 2012-04-06 09:10:38