Browse Source

Add index action to User controller

Frans Bergman 7 years ago
parent
commit
7a401d89e7

+ 8 - 0
app/controllers/users_controller.rb

@@ -3,6 +3,14 @@ class UsersController < ApplicationController
   before_action :set_user, only: [:show, :edit, :update, :destroy]
   before_action :correct_user, only: [:edit, :update]
 
+  def index
+    respond_to do |format|
+      @users = User.all
+      format.json
+      format.html
+    end
+  end
+
   def show
   end
 

+ 26 - 0
app/views/users/index.html.erb

@@ -0,0 +1,26 @@
+<% provide(:title, "Users") %>
+<h1>Users</h1>
+<table width="100%" class="table table-striped table-bordered table-hover">
+  <thead>
+    <tr>
+      <th>
+        Picture
+      </th>
+      <th>
+        Name
+      </th>
+    </tr>
+  </thead>
+  <tbody>
+    <% for user in @users %>
+      <tr>
+        <td>
+          <%= image_tag user.picture.small.url %>
+        </td>
+        <td>
+          <%= link_to user.name, user %>
+        </td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>

+ 4 - 0
app/views/users/index.json.jbuilder

@@ -0,0 +1,4 @@
+json.array! @users do |user|
+  json.id user.id
+  json.name user.name
+end