feat: add user index page and update navigation

- Implement users index action in UsersController
- Create view for displaying all users with gravatar
- Update header to link to users index
- Add styles for user list display

This commit introduces a new feature that allows logged-in users to
view a list of all registered users. It includes necessary
controller actions, view templates, and styling to improve the
user interface.
This commit is contained in:
songtianlun 2025-01-05 01:59:05 +08:00
parent 97c91fc8f3
commit cf76756f55
6 changed files with 38 additions and 3 deletions

View File

@ -2,6 +2,7 @@
/* color */ /* color */
$light-gray: #777; $light-gray: #777;
$gray-lighter: #D3D3D3;
/* universal */ /* universal */
body { body {
@ -187,3 +188,14 @@ input {
width: auto; width: auto;
margin-left: 0; margin-left: 0;
} }
/* Users index */
.users {
list-style: none;
margin: 0;
li {
overflow: auto;
padding: 10px 0;
border-bottom: 1px solid $gray-lighter;
}
}

View File

@ -1,7 +1,12 @@
class UsersController < ApplicationController class UsersController < ApplicationController
include SessionsHelper include SessionsHelper
before_action :logged_in_user, only: [ :edit, :update ] before_action :logged_in_user, only: [ :index, :edit, :update ]
before_action :correct_user, only: [ :edit, :update ] before_action :correct_user, only: [ :edit, :update ]
def index
@users = User.all
end
def show def show
@user = User.find(params[:id]) @user = User.find(params[:id])
# debugger # debugger

View File

@ -1,5 +1,6 @@
module UsersHelper module UsersHelper
def gravatar_for(user, size: 80) def gravatar_for(user, options = { size: 80 })
size = options[:size]
gravatar_id = Digest::MD5.hexdigest(user.email.downcase) gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar") image_tag(gravatar_url, alt: user.name, class: "gravatar")

View File

@ -18,7 +18,7 @@
<li><%= link_to "Home", root_url %></li> <li><%= link_to "Home", root_url %></li>
<li><%= link_to "Help", help_url %></li> <li><%= link_to "Help", help_url %></li>
<% if logged_in? %> <% if logged_in? %>
<li><%= link_to "Users", '#' %></li> <li><%= link_to "Users", users_path %></li>
<li class="dropdown"> <li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b> Account <b class="caret"></b>

View File

@ -0,0 +1,12 @@
<% provide(:title, 'All users') %>
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>

View File

@ -10,6 +10,11 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
end end
test "should redirect index when not logged in" do
get users_path
assert_redirected_to login_url
end
test "Should get sign up title" do test "Should get sign up title" do
get signup_path get signup_path
assert_response :success assert_response :success