SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined Method New Error

    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:

    Code:
    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

    Code:
    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

    Code:
    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 view

    Code:
    <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>
    Contact Model

    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
    Any help people can offer to fix this error really would be much appreciated because I am totally stuck.

    Many thanks in advance for your help!

    Tom

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,282
    Mentioned
    180 Post(s)
    Tagged
    4 Thread(s)
    If that is your entire Routes file, you do NOT have a route defined for "/contact/new"
    Is that the URI you are using when you get the error?
    Based on the definitions in your Routes file you need to use "/newcontact/" Do you get the error when you use that URI?
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey ParkinT,

    Thanks so much for your reply. I have been using the URL Localhost:3000/newcontact as is named in the routes file and sadly I still get the error.

    Do you have an idea of what might be causing this?

    Thanks again for your help,
    Tom

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Could you post the stack trace or is that the only thing that shows?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Scannon,

    The application Trace is just the error I already mentioned.

    If I click on Full Trace I get the following:

    Code:
    app/controllers/contact_controller.rb:3:in `new'
    actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (3.1.1) lib/abstract_controller/base.rb:167:in `process_action'
    actionpack (3.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (3.1.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    activesupport (3.1.1) lib/active_support/callbacks.rb:416:in `_run__312532209__process_action__116948142__callbacks'
    activesupport (3.1.1) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
    activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (3.1.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
    actionpack (3.1.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
    actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument'
    activesupport (3.1.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument'
    actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    actionpack (3.1.1) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
    activerecord (3.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (3.1.1) lib/abstract_controller/base.rb:121:in `process'
    actionpack (3.1.1) lib/abstract_controller/rendering.rb:45:in `process'
    actionpack (3.1.1) lib/action_controller/metal.rb:193:in `dispatch'
    actionpack (3.1.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
    actionpack (3.1.1) lib/action_controller/metal.rb:236:in `block in action'
    actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `call'
    actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
    actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:29:in `call'
    rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
    rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
    rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
    rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
    rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
    actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:532:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
    rack (1.3.6) lib/rack/etag.rb:23:in `call'
    rack (1.3.6) lib/rack/conditionalget.rb:25:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/head.rb:14:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/flash.rb:243:in `call'
    rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
    rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call'
    activerecord (3.1.1) lib/active_record/query_cache.rb:62:in `call'
    activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
    activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
    activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
    rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
    railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
    rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
    rack (1.3.6) lib/rack/runtime.rb:17:in `call'
    activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
    rack (1.3.6) lib/rack/lock.rb:15:in `call'
    actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
    railties (3.1.1) lib/rails/engine.rb:456:in `call'
    railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
    railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
    rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
    C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
    C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
    C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
    Thanks again for all your help.

    Tom

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I think the first thing that we need to do is work on your routes.rb file. We can cut that down to almost half and I think it is possible that you might have some conflicting routing going on.

    So, I would restructure the routes.rb file like this

    Code Ruby:
    Contact::Application.routes.draw do
      get "home" => "dashboard#home", :as => "home"
      get "logout" => "sessions#destroy", :as => "logout"
      get "login" => "sessions#new", :as => "login"
      get "signup" => "users#new", :as => "signup"
      root :to => "sessions#new"
     
     
      resources :users
      resources :sessions
      resources :contacts
    end

    This provides you with all of the functionality that you need for users, logging in/out, signing up, and managing contacts. The only difference is that you would add a new contact with the url of http://localhost.com/contact/new as opposed to http://localhost.com/newcontact. The former would be the "restful" way of doing it.

    Also, the following lines in your view should be changed. (newcontact_path should be new_contact_path)

    <div id="add-contact">
    <%= link_to "Add New Contact", new_contact_path %></div>



    See what happens if you make these changes. Also, when does this error appear? Right when you start the server and try to use your app or when you are trying to do something? If it is when you are trying to do something post exactly what you did to give us a better idea of what might be going on because outside of the two things I noticed above, I don't see any other errors.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Scannon,

    Thanks for your help again, I have implemented the new setup but am still facing the same error as before. The error is occurring when I hit the link on my Dashboard page to go through to the Adding a new contact view. The server is loading up fine and I can move between other pages without error - it's only when loading this one page 'Contact#new'

    The error I am still receiving is
    Code:
    undefined method `new' for Contact:Module
    My current setup is:

    Routes (

    Code:
    Contact::Application.routes.draw do
      get "home" => "dashboards#home", :as => "home"
      get "logout" => "sessions#destroy", :as => "logout"
      get "login" => "sessions#new", :as => "login"
      post "login" => "sessions#create"
      delete "logout" => "sessions#destroy"
      get "signup" => "users#new", :as => "signup"
      get "contactnew" => "contacts#new"
      root :to => "sessions#new"
     
     
      resources :users
      resources :sessions
      resources :contacts
    end
    dashboards#home (relevant part)

    Code:
    <div id="add-contact">s
    	 <%= link_to "Add New Contact", new_contact_path %>
    	</div>
    Contact Model (/contact.rb)

    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

    Contacts_controller.rb

    Code:
    class ContactsController < 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
    I really am stumped on this as I can see no reason why it won't work.

    Any further advice you can offer would be awesome!

    Cheers,
    Tom

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The first thing to check is your contacts controller. In the full stack trace it is looking for a file named contact_controller.rb when it should be contacts_controller.rb. (Though in your latest post you have it pluralized, so I'm not sure which it is.) Check this and if you have a contact_controller.rb I would delete it and have only contacts_controller.rb

    Once you are sure that the controller is pluralized and there is no singular version, and if the error continues, try commenting out the code inside the new method and see if that gets rid of the error message.

    I'm kind of stumped as well...I've never seen that error message before. :P

    As a final measure, you could always post your code to Github (if you use it) or send me a zip and I will take a look at the full code to see what is going on.

    Also, after a bit of searching, I have found that others have experienced this error when using a reserved word within their application. I don't see any in the code you posted, but it could be possible that you have something in another file. I'm thinking that at this point it would be best to see your entire app structure to see what is going on.
    Last edited by scannon; Apr 9, 2012 at 04:12. Reason: added to post

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Scannon,

    I finally managed to figure out what was going wrong and don't think I had given you enough information to work it out .

    Rails was getting confused between the module defined in application.rb (Contact) and my model class (also Contact). Apparently, you can't have module Contact in one place and class Contact in another.

    I had to rename one of them. In my case I renamed my application ContactApp to stop the confusion and everything started working!

    Thanks so much for all your help as always

    Tom


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •