I am creating a basic app for an address book and would like to associate Contacts with Users.
I have set up authentication for Users and am now in the process of trying to link my contacts table with the users. I have two seperate controllers, one for users and another for contacts and then my plan is to associate them through belongs_to:user and has_many :contacts.
I am absolutely stumped by an error I keep receiving though
undefined method `new' for Contact:Module
As far as I am aware I have defined new correctly in my contact controller but for some reason I can’t get this to work.
I have the following code at the moment
Routes:
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
Contact Controller
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
Contact/new Form
<%= 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 %>
Any help people can offer to fix this error really would be much appreciated because I am totally stuck! I think it might be something to do with pluralisation of the word contact but can’t seem to find the correct way to fix it.
Many thanks in advance for your help!
Tom
Rails is getting confused between the module defined in application.rb (Contact) and your model class (also Contact). You can’t have
module Contactin one place andclass Contactin another (in fact since you’re in development mode, the fact that the Contact module exists means that rails isn’t even looking at your contact.rb file)You’re going to have to rename one of them. The application one might be the easier one to change – just replace all instances of
Contact::Applicationwith (eg)ContactApp::Applicationand changemodule Contactin application.rb to moduleContactApp. You also barely ever type the name of the application module so it having a slightly dissonant name isn’t too much of a big deal.