skip to Main Content

When attempting to go to my Contact us page(/contact/new) I am getting the error “NoMethodError in Contact#new undefined method ‘contacts_path”. Is the issue with my routes?

Routes for Contact:

        contacts GET    /contacts(.:format)                   contacts#index
                       POST   /contacts(.:format)                   contacts#create
           new_contact GET    /contacts/new(.:format)               contacts#new
          edit_contact GET    /contacts/:id/edit(.:format)          contacts#edit
               contact GET    /contacts/:id(.:format)               contacts#show
                       PATCH  /contacts/:id(.:format)               contacts#update
                       PUT    /contacts/:id(.:format)               contacts#update
                       DELETE /contacts/:id(.:format)               contacts#destroy

Contact Page Controller:

class ContactController < ApplicationController

 def new
 @contact = Contact.new
 end

 def create
 @contact = Contact.new(contact_params)
 @contact.name =  contact.name
 @contact.email = contact.email
 @contact.message = contact.message
 @contact.company = contact.company

  if @contact.save
  flash[:notice] = "Your messsage has been sent"
  redirect_to '/'
   else
    flash[:error] = "Your message has not been sent please try again"
    render :new
  end
 end

 def contact_params
 params.require(:name, :email, :message).permit(:company)
 end
end

View for Contact page – new.html.erb:

<!DOCTYPE html>
 <html lang="en"> <!--<![endif]-->  
 <head>
<!-- Meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">    
<link rel="shortcut icon" href="favicon.ico">  
<link href='http://fonts.googleapis.com/css?family=Roboto:400,400italic,500,500italic,700,700italic,900,900italic,300italic,300' rel='stylesheet' type='text/css'> 
<link href='http://fonts.googleapis.com/css?family=Roboto+Slab:400,700,300,100' rel='stylesheet' type='text/css'>
<!-- Global CSS -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">   
<!-- Plugins CSS -->    
<link rel="stylesheet" href="assets/plugins/font-awesome/css/font-awesome.css">
<link rel="stylesheet" href="assets/plugins/flexslider/flexslider.css">
<!-- Theme CSS -->
<link id="theme-style" rel="stylesheet" href="assets/css/styles.css">

</head> 



    <div class="headline-bg contact-headline-bg">
    </div><!--//headline-bg-->


    <!-- ******Contact Section****** --> 
    <section class="contact-section section section-on-bg">
        <div class="container">
            <h2 class="title text-center">Contact Us</h2>
            <p class="intro text-center">We’d love to hear from you!   </p>
            <form id="contact-form" class="contact-form" method="post" action="">                    
                <div class="row text-center">
                    <div class="contact-form-inner col-md-8 col-sm-12 col-xs-12 col-md-offset-2 col-sm-offset-0 xs-offset-0">
                        <div class="row">      
                            <%= form_for @contact do |f| %>                         
                            <div class="col-md-6 col-sm-6 col-xs-12 form-group">


                                <%= f.label :name, class: 'sr-only' %>
                                <%= f.text_field :name, class: 'form-control', placeholder: "Your name" %>
                            </div>                    
                            <div class="col-md-6 col-sm-6 col-xs-12 form-group">
                                <%= f.label :email, class: 'sr-only' %>
                                <%= f.text_field :email, class: 'form-control', placeholder: "Your email address" %>

                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12 form-group">
                                <%= f.label :Company, class: 'sr-only' %>
                                <%= f.text_field :company, class: 'form-control', placeholder: "Your company's name" %>

                            </div>
                            <div class="col-md-12 col-sm-12 col-xs-12 form-group">
                                <%= f.label :Message, class: 'sr-only' %>
                                <%= f.text_area :body, rows: 12, class: 'form-control', placeholder: "Enter your message" %>

                            </div>
                             <div class="col-md-12 col-sm-12 col-xs-12 form-group">
                                <%= f.submit "Send Message", class: 'btn btn-block btn-cta btn-cta-primary' %>
                            </div>
                         <% end %>                           
                        </div><!--//row-->
                    </div>
                </div><!--//row-->
                <div id="form-messages"></div>
            </form><!--//contact-form-->
        </div><!--//container-->
    </section><!--//contact-section-->

    <!-- ******Other Contact Section****** --> 
    <section class="contact-other-section section">
        <div class="container text-center">
            <h2 class="title">Other ways to reach us</h2>
            <p class="intro">You can also get in touch lorem ipsum dolor sit amet. Donec ut massa consequat, bibendum metus pellentesque, pellentesque arcu. </p>
            <div class="row">
                <ul class="other-info list-unstyled col-md-6 col-sm-10 col-xs-12 col-md-offset-3 col-sm-offset-1 xs-offset-0" >
                    <li><i class="fa fa-envelope-o"></i><a href="#">[email protected]</a></li>
                    <li><i class="fa fa-twitter"></i><a href="https://twitter.com/Herewego" target="_blank">@SanSimon</a></li>
                    <li><i class="fa fa-phone"></i><a href="tel:+0800123456">0800 123 456</a></li>
                    <li><i class="fa fa-map-marker"></i>Queen Square <br /> 56 College Green Road<br />BS1 XR18<br />Bristol<br />UK</li>
                </ul>
            </div><!--//row-->
        </div><!--//container-->
    </section><!--//contact-other-section-->




<!-- ******FOOTER****** --> 
<div id="footer-bottom">
        <div class="container text-center">
            <p>© Copyright 2016 <a href="#">The Group</a>. All Rights Reserved.</p>
        </div>          
    </div>
</footer>




<script>
    /* ======= Fixed header when scrolled ======= */    
$(window).on('scroll load', function() {

     if ($(window).scrollTop() > 0) {
         $('#header').addClass('scrolled');
     }
     else {
         $('#header').removeClass('scrolled');

     }
});
</script>

2

Answers


  1. The problem is with the singular naming for your controller.

    You could run

    rails d controller contact

    to delete the contact Contact controller.

    Then you could run

    rails g controller contracts

    to generate the new Contracts controller. Copy and paste what you had in the ContactController and also check that your routes are updates as well.

    Login or Signup to reply.
  2. It seems like you have ambiguous routes, controller and views.
    @contact in your form will generate the contacts_path as default

    You can do this by following these steps:
    Change the routes.rb file

    routes.rb

    resources :contacts
    

    Change your controller into plural name

    rails generate controller contacts
    

    Then, restart your server.

    I hope this helps you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search