mardi 11 octobre 2022

ActionController::RoutingError (uninitialized constant ContactsController Object.const_get(camel_cased_word) Did you mean? ContactController

I am a newbie to web development and currently learning. I have a problem: when I click the submit button, it doesn't push the values into the Hash and shows me the following error message:

Started POST "/contacts" for 49.37.171.198 at 2022-10-11 13:38:17 +0000
Cannot render console from 49.37.171.198! Allowed networks: 127.0.0.0/127.255.255.255, ::1
  ActiveRecord::SchemaMigration Pluck (0.5ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  
ActionController::RoutingError (uninitialized constant ContactsController

      Object.const_get(camel_cased_word)
            ^^^^^^^^^^
Did you mean?  ContactController

            raise MissingController.new(error.message, error.name)
            ^^^^^):

here in this controller.rb file when i create a global variable with the Contact.new (which i think is the class from Model files) ,it dosen't create a hash with values after clicking the submit button and gives me the above errors

contacts_controller.rb

class ContactsController < ApplicationController
  
  def new
    @Contact = Contact.new
  end
  
  def Create
    @Contact = Contact.new(contact_params)
    
    if @Contact.save
      flash[:success] = "Message Successfully Sent"
      redirect_to new_contact_path
      
    else
      flash[:danger] = @Contact.errors.full_messages.join(", ")
      redirect_to new_contact_path
      
    end
  end
    
    private
      def contact_params
        params.require(:contact).permit(:Name, :Email, :Comments) 
      end
      
end

new.html.erb

<div class="container">
  <div class='row'>
    <h3 class="container text-center contact-head3">Contact Us.</h3>
        
        
    <div class='col-md-5 col-md-offset-5'>

      <div class='well'>
        
        <%= form_for @Contact do |f| %>
                        
          <div class='form-group'>
            <%= f.label :Name %>
            <%= f.text_field :Name , class:'form-control' %>
          </div>
                    
          <div class='form-group'>
            <%= f.label :Email %>
            <%= f.text_field :Email, class:'form-control' %>
          </div>
                    
          <div class='form-group'>
            <%= f.label :Comments %>
            <%= f.text_area :Comments, class:'form-control' %>
          </div>
                        
          <div>
            <%= f.submit 'Submit', class:'btn btn-default' %>
          </div>
                    
        <% end %>
      </div>
    </div>
</div>
    

</div>

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Sapna Xerox</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
    
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>

    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
    <%= javascript_importmap_tags %>
  </head>

  <body>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
    <div class = "outer-navbar-sides">
      <div class='container'>
        <ul class=" nav nav-tabs">
    
          <li><%= link_to "Home",root_path%></li>
          <li><%= link_to "About",about_path%></li>
          <li><%= link_to "Contact",new_contact_path %></li>
    
        </ul>
      </div>
    </div>
    
    <div class="padding-fixer-of-container">
      <main class="container mx-auto mt-28 px-5 flex header">
    </div>
    
    <div class='container'>
      <% flash.each do |type,msg| %>
        <%= content_tag :div, msg, class:"alert alert #{type}" %>
      <% end %>
    </div>
    
      
      <%= yield %>
    </main>
  </body>
</html>

Contact.rb

    class Contact < ActiveRecord::Base
      
    end

routes.rb

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
  
  root to: 'pages#home'
  get '/about', to: 'pages#about'
  resources :contacts, only: [:create]
  get 'contact-us', to: 'contact#new', as: 'new_contact'
end

Aucun commentaire:

Enregistrer un commentaire