Hey,
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:
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.Code:undefined method `new' for Contact:Module
I have the following code at the moment:
Routes
Contact ControllerCode: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#New viewCode: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 ModelCode:<div class="column-1"> <div id="logo"> <a href="/home"><img id="logo" src="/assets/logo_contacts_small.png" alt="Connect_Logo" style="width=100%" height="100%" /> </a> </div> <div id="search"> </div> <div id="add-contact">s <%= link_to "Add New Contact", newcontact_path %> </div> <div class="signout"> <li> <% if current_user %> <%= link_to "Logout", logout_path %> <% else %> <%= render login_path %> <% end %></li> </div> </div> <div class="column-2"> <div id="contactnew-area"> <div class="contactnew-fields"> <%= 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 %> </div> </div>
Any help people can offer to fix this error really would be much appreciated because I am totally stuck.Code:class Contact < ActiveRecord::Base belongs_to :user validates_presence_of :name validates_presence_of :email validates_length_of :email, :within => 6..50 validates_uniqueness_of :email, :case_sensitive => false, :on => :create validates_format_of :email, :with => /^[A-Z0-9_.%-]+@([A-Z0-9_]+\.)+[A-Z]{2,4}$/i, end
Many thanks in advance for your help!
Tom![]()



Reply With Quote


Bookmarks