Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting error as Unable to autoload constant UsersController

I am getting an error while visiting /users/new in Rails 4 as Unable to autoload constant UsersController, expecting /app/controllers/users_controller.rb to define it.

Here is the controller code

    class UserController < ApplicationController
     def new
      @user = User.new
     end

     def create
      @user = User.new(params[:user]).permit(:email,  :password,:password_confirmation)

      respond_to do |format|
       if @user.save
        format.html { redirect_to new_user_path, notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
       else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
       end
      end
    end 
  end

And the view for new.html.erb I have is:

  <h1>Sign up</h1>

  <%= form_for(@user)  do |f| %>

    <% if @user.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@user.errors.count, "error") %> prohibited this post from being      saved:</h2>

        <ul>
          <% @user.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
        </ul>
     </div>
   <% end %>
   <div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
   </div>
   <div class="field">
    <%= f.label :password %><br>
    <%= f.password_field :password %>
   </div>
   <div class="field">
    <%= f.label :password_confirmation, "Confirmation" %>
    <%= f.password_field :password_confirmation %>
   </div>
   <div class="actions">
    <%= f.submit "Create my account" %>
   </div>
 <% end %>

User model:

class User < ActiveRecord::Base
  has_many :projects
  has_many :pledges
  has_many :posts
  has_many :comments
end
like image 553
Preethika Avatar asked Sep 30 '13 02:09

Preethika


1 Answers

Rails expects controller names to be pluralized. The very first line of the the first file contents you posted is written as singular:

In /app/controllers/users_controller.rb you have:

class UserController < ApplicationController

Instead, it should be:

class UsersController < ApplicationController


This Rails Guide provides an example for defining a resource in your routes file. Further, there is an informational note that explains that defining a routes with the resource method will always map to the pluralized name of the controller.

This is the informational note from the guide.

Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers. So that, for example, resource :photo and resources :photos creates both singular and plural routes that map to the same controller (PhotosController).

source: Resource Routing: The Rails Default

like image 165
sealocal Avatar answered Nov 15 '22 23:11

sealocal