title: Devise - Listing Users
body: <p>in my rails app I need to list the users by username, the error I get is</p>
  
  <pre><code>NoMethodError in Posts#index
  
  Showing C:/Users/Corey/Dev/statlog/app/views/posts/index.html.erb where line #12 raised:
  
  undefined method `username' for #&lt;Array:0x3893298&gt;
  Extracted source (around line #12):
  
  9: &lt;div class="follow-row"&gt;
  10:   &lt;div class="titan-users nuvo"&gt;&lt;h2&gt;TITAN Users&lt;/h2&gt;&lt;/div&gt;
  11:     &lt;% @users.each do |post| %&gt;
  12:       &lt;div class="user-row"&gt;&lt;%= @user.username %&gt;&lt;/div&gt;
  13:     &lt;% end %&gt;
  14: &lt;/div&gt;
  15: 
  Rails.root: C:/Users/Corey/Dev/statlog
  
  Application Trace | Framework Trace | Full Trace
  app/views/posts/index.html.erb:12:in `block in _app_views_posts_index_html_erb___134739565_23193672'
  app/views/posts/index.html.erb:11:in `each'
  app/views/posts/index.html.erb:11:in `_app_views_posts_index_html_erb___134739565_23193672'
  app/controllers/posts_controller.rb:13:in `index'
  </code></pre>
  
  <p>Down below is my posts_controller ya see I don't have a user_controller.</p>
  
  <pre><code>    class PostsController &lt; ApplicationController
    # GET /posts
    # GET /posts.json
    def index
      @posts = Post.all(:order =&gt; "created_at DESC")
      @post = Post.new
  
      @users = User.all
  
  
      @user = User.find(:all)
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @posts }
      end
    end
  
    # GET /posts/1
    # GET /posts/1.json
    def show
      redirect_to posts_path
    end
  
    # GET /posts/new
    # GET /posts/new.json
    def new
      @post = Post.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @post }
      end
    end
  
    # GET /posts/1/edit
    def edit
      @post = Post.find(params[:id])
    end
  
    # POST /posts
    # POST /posts.json
    def create
      @post = current_user.posts.build(params[:post])
  
      respond_to do |format|
        if @post.save
          format.html { redirect_to @post, notice: 'Post was successfully created.' }
          format.json { render json: @post, status: :created, location: @post }
        else
          format.html { render action: "new" }
          format.json { render json: @post.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PUT /posts/1
    # PUT /posts/1.json
    def update
      @post = Post.find(params[:id])
  
      respond_to do |format|
        if @post.update_attributes(params[:post])
          format.html { redirect_to @post, notice: 'Post was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @post.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /posts/1
    # DELETE /posts/1.json
    def destroy
      @post = Post.find(params[:id])
      @post.destroy
  
      respond_to do |format|
        format.html { redirect_to posts_url }
        format.json { head :no_content }
      end
    end
  end
  </code></pre>
  
  <p>Here is my user and post model.</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    attr_accessible :status, :author, :email, :username
  
    belongs_to :user
  
    validates :status, :presence =&gt; true
  
  
  
  end
  </code></pre>
  
  <p>My user model</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :token_authenticatable, :confirmable,
    # :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    # Setup accessible (or protected) attributes for your model
    attr_accessible :email, :password, :password_confirmation, :remember_me, :username
  
    has_many :posts
    # attr_accessible :title, :body
  end
  </code></pre>
  
  <p>Last but not least my post/index.html.erb view.</p>
  
  <pre><code>&lt;% if user_signed_in? %&gt;
    &lt;h1 id="welcome" class="nuvo"&gt;Welcome &lt;%= current_user.username %&gt;!&lt;/h1&gt;
  &lt;% else %&gt;
    &lt;h1 id="welcome" class="nuvo"&gt;Log-In to make some posts!&lt;/h1&gt;
  &lt;% end%&gt;
  
  &lt;div class="follow-row"&gt;
    &lt;div class="titan-users nuvo"&gt;&lt;h2&gt;TITAN Users&lt;/h2&gt;&lt;/div&gt;
      &lt;% @users.each do |post| %&gt;
        &lt;div class="user-row"&gt;&lt;%= @user.username %&gt;&lt;/div&gt;
      &lt;% end %&gt;
  &lt;/div&gt;
  
  &lt;div class="statuses"&gt;
    &lt;% if user_signed_in? %&gt;&lt;div class="status-form"&gt;&lt;%= render 'form' %&gt;&lt;/div&gt;&lt;% end %&gt;
    &lt;% @posts.each do |post| %&gt;
      &lt;div class="post"&gt;
        &lt;div class="tstamp"&gt;&lt;strong&gt;Posted &lt;%= time_ago_in_words(post.created_at) %&gt; ago by &lt;%= post.user.username %&gt;&lt;/strong&gt;&lt;/div&gt;
        &lt;div class="status"&gt;&lt;%= post.status %&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My user migration file</p>
  
  <pre><code>class DeviseCreateUsers &lt; ActiveRecord::Migration
    def change
      create_table(:users) do |t|
        ## Database authenticatable
        t.string :email,              :null =&gt; false, :default =&gt; ""
        t.string :encrypted_password, :null =&gt; false, :default =&gt; ""
  
        ## Recoverable
        t.string   :reset_password_token
        t.datetime :reset_password_sent_at
  
        ## Rememberable
        t.datetime :remember_created_at
  
        ## Trackable
        t.integer  :sign_in_count, :default =&gt; 0
        t.datetime :current_sign_in_at
        t.datetime :last_sign_in_at
        t.string   :current_sign_in_ip
        t.string   :last_sign_in_ip
  
        ## Confirmable
        # t.string   :confirmation_token
        # t.datetime :confirmed_at
        # t.datetime :confirmation_sent_at
        # t.string   :unconfirmed_email # Only if using reconfirmable
  
        ## Lockable
        # t.integer  :failed_attempts, :default =&gt; 0 # Only if lock strategy is :failed_attempts
        # t.string   :unlock_token # Only if unlock strategy is :email or :both
        # t.datetime :locked_at
  
        ## Token authenticatable
        # t.string :authentication_token
  
  
        t.timestamps
      end
  
      add_index :users, :email,                :unique =&gt; true
      add_index :users, :reset_password_token, :unique =&gt; true
      # add_index :users, :confirmation_token,   :unique =&gt; true
      # add_index :users, :unlock_token,         :unique =&gt; true
      # add_index :users, :authentication_token, :unique =&gt; true
    end
  end
  </code></pre>
  
  <p>Add username to user migration file </p>
  
  <pre><code>class AddUsernameToUser &lt; ActiveRecord::Migration
    def change
      add_column :users, :username, :string
    end
  end
  </code></pre>