wyc
wyc

Reputation: 55273

Viewing users (index, show, edit, etc.) in a Rails app that uses Devise?

I'm building a Rails application using Devise. Devise is all set up even omniauth.

controllers/users_controller.rb:

class UsersController < ApplicationController
  def index
    @users = User.all
  end
end

models/user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username

  has_many :microposts
  has_many :comments

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    if user = User.where(:email => data.email).first
      user
    else # Create a user with a stub password. 
      User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end
  end
end

views/_user.html.erb:

<div>
<h2>Username: <%= user.username %></h2>
<p>Email: <%= user.email %></p>
<p>ID: <%= user.id %></p>
<br />
</div>

views/index.html.erb:

<h2>User index</h2>
<p>microposts <%= render @users %></p>

But when I access http://localhost:3000/users, I get:

No route matches [GET] "/users"

Do I have to set the views in the views/devise folder instead? In the views/devise/sessions folder or something like that?

Upvotes: 2

Views: 2141

Answers (1)

Ben Zhang
Ben Zhang

Reputation: 1281

Have you tried adding

resources :users

to your routes.rb?

Upvotes: 5

Related Questions