用户管理②:设定一览

用户管理②

Header10

0 : 这个章的目的

 看看「用户管理②」里设定的內容


1 : 设定一览

在「用户管理②」里,做了「users」网页的设定。
为了做「users」网页的设定,打入

 terminal
 使用Windows的话,请使用「命令提示符」吧。

1 ralis g controller users

之后,用这个命令作好的
  • 做显示文件的「app > views > users」文件夾
  • 做资料操作的「app > controllers > users_controller.rb」文件


3. 设定使用哪一个网页的设定文件「config > route.rb

这个三个设定网页。
 七个网页的设定(route.rb)是,

 route.rb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... 61 62 Rails.application.routes.draw do devise_for :users resources :posts , :only => [:show, :index, :new, :create, :edit, :update, :destroy] resources :welcome, :only => [:index] root "welcome#index" resources :users, :only => [:show, :index, :edit, :update] # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" # root 'welcome#index' # Example of regular route: # get 'products/:id' => 'catalog#view' # 省略 end

 资料操作的设定(controller文件)是,

 users_controller.rb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class UsersController < ApplicationController before_action :authenticate_user!, :only => [:show, :index, :edit, :update] def show @user = User.find(params[:id]) end def index @users = User.all end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) @user.update(params.require(:user).permit(:first_name, :family_name, :self_introduction)) end end

 网页显示的设定(views文件夾)是,在
  • show.html.erb
  • index.html.erb
  • edit.html.erb
  • update.html.erb
这个四个设定文件里,设定显示。
 show.html.erb
1 2 3 4 5 6 7 8 <h1>会员资料</h1> <hr> <p>名字:<%= @user.family_name %><%= @user.first_name %></p> <p>自我介绍:<%= @user.self_introduction %></p> <hr> <p><a href="/posts" class="btn btn-info">返回主页</a></p> <p><a href="/users" class="btn btn-primary">返回会员一览</a></p> <p><a href="/users/<%= @user.id %>/edit" class="btn btn-success">编辑会员资料</a>

 index.html.erb
1 2 3 4 5 6 7 <h1>会员一览</h1> <hr> <% @users.each do |u| %> <p><a href="/users/<%= u.id %>"><%= u.family_name %><%= u.first_name %></a></p> <hr> <% end %> <a href="/posts" class="btn btn-info">返回主页</a>

 edit.html.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <h1>编辑会员资料</h1> <hr> <%= form_for @user do |f| %> <p>姓名</p> <%= f.text_field :family_name, :class => "form-control my-form" %> <p>名字</p> <%= f.text_field :first_name, :class => "form-control my-form" %> <p>自我介绍</p> <%= f.text_area :self_introduction, :class => "form-control my-form" %> <hr> <%= f.submit "登录", :class => "btn btn-primary" %> <% end %> <hr> <a href="/users/<%= @user.id %>" class="btn btn-info">返回会员资料</a>

 update.html.erb
1 2 3 4 5 6 7 8 <h1>会员资料编辑好了。</h1> <hr> <p>名字:<%= @user.family_name %><%= @user.first_name %></p> <p>自我介绍:<%= @user.self_introduction %></p> <hr> <p><a href="/users/<%= @user.id %>" class="btn btn-primary">返回会员资料</a></p> <p><a href="/users/<%= @user.id %>/edit" class="btn btn-success">编辑会员资料</a></p> <p><a href="/posts" class="btn btn-info">返回主页</a></p>

 用会员有沒有登入的条件判別更改显示navigation-bar的方法,编辑「application.html.erb

 application.html.erb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <!DOCTYPE html> <html> <head> <title>Myapp</title> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> </head> <body> <nav id="mainNav" class="navbar navbar-default navbar-fixed-top"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="/posts" class="navbar-brand">myapp</a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-right"> <% if user_signed_in? %> <li><a href="/users">會員一覽</a></li> <li><a href="/users/<%= current_user.id %>">會員資料</a></li> <li><a href="/posts/new">作投稿</a></li> <li><%= link_to "登出", destroy_user_session_path, :method => :delete %></li> <% else %> <li><a href="/users/sign_up">註冊</a></li> <li><a href="/users/sign_in">登入</a></li> <% end %> </ul> </div> </div> </nav> <div class="container main-page"> <% if notice %> <br> <p class="alert alert-success"><%= notice %></p> <% end %> <% if alert %> <br> <p class="alert alert-danger"><%= alert %></p> <% end %> <%= yield %> </div> </body> </html>