MaynardOne
MaynardOne

Reputation: 57

Can't add user: ActionController::RoutingError (uninitialized constant UsersController)

So, I'm at chapter 7.2 Signup form in Hartls tutorial but I have run into some issues. I don't get the ForbiddenAttributes (Fig 7.15) as in the tutorial.

I can access my /signup but whenever I submit a user with wrong attributes (mail/password) it throws a 500, "We're sorry, but something went wrong.". Not the above mentioned ForbiddenAttributes.

A snippet from the server log:

Started POST "/users" for ::1 at 2015-08-01 22:19:59 +0200

ActionController::RoutingError (uninitialized constant UsersController):
  activesupport (4.2.0) lib/active_support/inflector/methods.rb:261:in `const_get'
  activesupport (4.2.0) lib/active_support/inflector/methods.rb:261:in `block in constantize'
  activesupport (4.2.0) lib/active_support/inflector/methods.rb:259:in `each'
  activesupport (4.2.0) lib/active_support/inflector/methods.rb:259:in `inject'
  activesupport (4.2.0) lib/active_support/inflector/methods.rb:259:in `constantize'

I have narrowed it down to the resources: users in my routes.rb. But can't pinpoint it. Reason I narrowed it down is whenever I interchange 'users' with 'user' the /signup stop working and I get this instead

if options.empty?
  recipient.send(method, *args)
else
  recipient.send(method, *args, options)
end

While /signup stops working my users/1 (show) starts working which it does not do with 'users' in route.

Code:

Routes.rb

Rails.application.routes.draw do

  resources :user

  root              'static_pages#home'
  get 'help' =>     'static_pages#help'
  get 'about' =>    'static_pages#about'
  get 'contact' =>  'static_pages#contact'
  get 'signup' =>   'user#new'
  get 'users/:id' => 'user#show'

end

user_controller.rb

class UserController < ApplicationController

  def index
  end

  def show
    @user = User.find(params[:id])
  end

  def new
   @user = User.new
  end

  def create
    @user = User.new(params[:users])
    if @user.save
      #Successfull
    else
      render 'new'
    end
  end

end

User.rb

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                            format: { with: VALID_EMAIL_REGEX },
                            uniqueness: { case_sensetive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end

Upvotes: 2

Views: 238

Answers (1)

Athar
Athar

Reputation: 3268

Issue is with UserController, as your code expects UsersController for action.

Controller names are always in plural as per convention. http://guides.rubyonrails.org/action_controller_overview.html#controller-naming-convention

try changing the UserController to UsersController

secondly change the resources :user to resources :users Also inside your app/view folder there is a folder named user. you need to rename that folder to users

Rather generate controller for users. and delete the one for user but that might lose your data. you need to make sure of copy stuff from user to users files.

Things will work.

Upvotes: 2

Related Questions