hcarreras
hcarreras

Reputation: 4612

Routing Error uninitialized constant when I create a new action

I know it is a typical problem. But I was just trying the routing in rails and I get this error:

uninitialized constant UsersController

And I don't know well where is the problem. The resume of my app is a simple application of a library, where users can book books and see the books they've booked. Here is my routes.rb

Brickstest2::Application.routes.draw do
  resources :books

  root "pages#home"    
  get "home", to: "pages#home", as: "home"
  get "inside", to: "pages#inside", as: "inside"


  devise_for :users

  namespace :admin do
    root "base#index"
    resources :users
  end

  resources :books do
    get :book_a_book, :as => "reserve"
  end

  resources :users do
    get :booked_books, :as => "reserved", :on => :member
  end

end

Actually When I do rake routes I get:

reserved_user_path
    GET  /users/:id/booked_books(.:format)   users#booked_books

And in users_controllers.rb I have:

 def booked_books
    @user = User.first(params[:user_id])
    #return unless @user == current_user
    @books = Books.where(:user = @user)
  end

The link to booked_books:

<%= link_to "My books", user_reserved_path(:user_id => current_user.id)%>

And finally my users/booked_books.html.erb:

<h1>My books</h1>
<%= @books.each do |book|%>
    <p><%= book.name %>
<% end %>

Thanks in advance.

Upvotes: 0

Views: 639

Answers (1)

zeantsoi
zeantsoi

Reputation: 26193

Have you declared a controller for your User resource that subclasses ApplicationController?

# app/controllers/users_controller.rb
class UsersController < ApplicationController

Note that the precise naming convention for a plural resource route for the User model is UsersController (note the s). I'm guessing that you've named your controller class UserController, which won't work for your use-case.

UPDATE:

Since you've declared a controller namespace, you'll want to declare your controller class like so:

# app/controllers/admin/users_controller.rb
class Admin::UsersController < ApplicationController

This should resolve any uninitialized constant UsersController errors involving your namespace.

Upvotes: 1

Related Questions