jeudi 30 juin 2016

ROR uninitialized constant ContatosController::Contato

I am starting to develop in ROR. The user history that I am doing now is a Contact Page. The codes for the MVC are listed below:

app/controller/contatos_controller.rb

class ContatosController < ApplicationController

  def new
    @contato = Contato.new
  end

  def create
    @contato = Contato.new(secure_params)
    if @contato.valid?
      flash[:notice] = "Mensagem enviada de #{@contato.name}."
      redirect_to root_path
    else
      render :new
    end
  end

  private

  def secure_params
    params.require(:contato).permit(:name, :subject, :email, :content)
  end

end

app/models/Contato.rb

class Contato
  include ActiveModel::Model
  attr_accessor :name, :string
  attr_accessor :subject, :string
  attr_accessor :email, :string
  attr_accessor :content, :string

  validates_presence_of :name
  validates_presence_of :subject
  validates_presence_of :email
  validates_presence_of :content
  validates_format_of :email,
    with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
  validates_length_of :content, :maximum => 500

end

app/views/contatos/new.html.erb

<h3>Contato</h3>
<div class="form">
<%= simple_form_for @contato do |form| %>
<%= form.error_notification %>
<%= form.input :name, autofocus: true %>
<%= form.input :subject %>
<%= form.input :email %>
<%= form.input :content, as: :text %>
<%= form.button :submit, 'Submit', class: 'submit' %>
<% end %>
</div>

config/routes.rb

Rails.application.routes.draw do
  resources :contatos, only: [:new, :create]
  root 'static_pages#home'
end

When I try to access http://localhost:3000/contatos/new, the following error is displayed:

NameError in ContatosController#new
uninitialized constant ContatosController::Contato
app/controllers/contatos_controller.rb:4:in `new'

What I found about this error is that it is related to typos, but this does not seem to be my case. It's probably a silly mistake, but I could not find it. Can anybody help me?

Aucun commentaire:

Enregistrer un commentaire