Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e7557e

Browse files
committedMar 14, 2019
管理者を変更できるように
1 parent 850ca08 commit 2e7557e

File tree

5 files changed

+71
-83
lines changed

5 files changed

+71
-83
lines changed
 

‎app/controllers/users_controller.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class UsersController < ApplicationController
22
include Common
33
before_action :logged_in_user, only: [:index, :show, :edit, :update, :destroy]
44
before_action :correct_user, only: [:edit, :update]
5-
before_action :admin_user, only: [:destroy]
5+
before_action :admin_user, only: [:destroy, :to_admin]
66
before_action :set_entrance_year, only: [:new, :edit, :create, :update]
77
before_action :set_open_year, only: [:show]
88

@@ -65,6 +65,16 @@ def destroy
6565
redirect_to users_url
6666
end
6767

68+
def change_admin
69+
@user = User.find(params[:id])
70+
if @user.update(admin: params[:admin])
71+
flash[:success] = "変更されました"
72+
else
73+
flash[:danger] = "失敗"
74+
end
75+
redirect_to @user
76+
end
77+
6878
private
6979

7080
def user_params

‎app/views/users/show.html.erb

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
<h1 class="center">
77
<%= @user.name %>
8+
<% if current_user.admin? %>
9+
<% if @user.admin? %>
10+
(admin)
11+
<%= link_to "管理者から外す", change_admin_user_path(@user, admin: false), method: :put, class: "btn btn-danger" unless @user == current_user %>
12+
<% else %>
13+
<%= link_to "管理者に設定", change_admin_user_path(@user, admin: true), method: :put, class: "btn btn-primary" %>
14+
<% end %>
15+
<% end %>
816
</h1>
917

1018
<%= form_tag user_url(@user), method: "GET", class: "form-horizontal" do%>

‎config/routes.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
delete 'logout' => 'sessions#destroy'
1010
get 'attend' => 'attendances#new'
1111

12-
resources :users
12+
resources :users do
13+
member do
14+
put :change_admin
15+
end
16+
end
1317
resources :account_activations, only: [:edit]
1418
resources :password_resets, only: [:new, :create, :edit, :update]
1519
resources :soukais

‎db/migrate/20170105102859_add_share_thing_content_type_datas.rb

-6
This file was deleted.

‎db/schema.rb

+47-75
Original file line numberDiff line numberDiff line change
@@ -14,106 +14,78 @@
1414
ActiveRecord::Schema.define(version: 20170601062944) do
1515

1616
create_table "attendances", force: :cascade do |t|
17-
t.integer "soukai_id"
18-
t.integer "user_id"
19-
t.datetime "created_at", null: false
20-
t.datetime "updated_at", null: false
21-
end
22-
23-
create_table "common_thing_content_types", force: :cascade do |t|
24-
t.datetime "created_at", null: false
25-
t.datetime "updated_at", null: false
26-
end
27-
28-
create_table "common_things", force: :cascade do |t|
29-
t.text "content"
30-
t.datetime "created_at", null: false
31-
t.datetime "updated_at", null: false
32-
t.string "name"
33-
t.integer "content_type_id"
17+
t.integer "soukai_id", limit: 4
18+
t.integer "user_id", limit: 4
19+
t.datetime "created_at", null: false
20+
t.datetime "updated_at", null: false
3421
end
3522

3623
create_table "events", force: :cascade do |t|
37-
t.string "name"
38-
t.integer "category_id"
39-
t.integer "season_id"
40-
t.datetime "created_at", null: false
41-
t.datetime "updated_at", null: false
24+
t.string "name", limit: 255
25+
t.integer "category_id", limit: 4
26+
t.integer "season_id", limit: 4
27+
t.datetime "created_at", null: false
28+
t.datetime "updated_at", null: false
4229
end
4330

4431
create_table "project_options", force: :cascade do |t|
45-
t.string "name"
46-
t.integer "price"
47-
t.integer "project_id"
48-
t.text "remarks"
49-
t.datetime "created_at", null: false
50-
t.datetime "updated_at", null: false
32+
t.string "name", limit: 255
33+
t.integer "price", limit: 4
34+
t.integer "project_id", limit: 4
35+
t.text "remarks", limit: 65535
36+
t.datetime "created_at", null: false
37+
t.datetime "updated_at", null: false
5138
end
5239

5340
create_table "projects", force: :cascade do |t|
54-
t.string "name"
55-
t.integer "soukai_id"
56-
t.datetime "created_at", null: false
57-
t.datetime "updated_at", null: false
58-
t.integer "user_id"
59-
end
60-
61-
create_table "share_thing_content_types", force: :cascade do |t|
62-
t.string "name"
63-
t.datetime "created_at", null: false
64-
t.datetime "updated_at", null: false
65-
end
66-
67-
create_table "share_things", force: :cascade do |t|
68-
t.string "name"
69-
t.integer "content_type_id"
70-
t.text "content"
71-
t.integer "user_id"
72-
t.datetime "created_at", null: false
73-
t.datetime "updated_at", null: false
41+
t.string "name", limit: 255
42+
t.integer "soukai_id", limit: 4
43+
t.datetime "created_at", null: false
44+
t.datetime "updated_at", null: false
45+
t.integer "user_id", limit: 4
7446
end
7547

7648
create_table "soukais", force: :cascade do |t|
77-
t.string "name"
49+
t.string "name", limit: 255
7850
t.date "date"
79-
t.string "password_digest"
80-
t.datetime "created_at", null: false
81-
t.datetime "updated_at", null: false
51+
t.string "password_digest", limit: 255
52+
t.datetime "created_at", null: false
53+
t.datetime "updated_at", null: false
8254
end
8355

8456
create_table "user_remind_event_categories", force: :cascade do |t|
85-
t.integer "user_id"
86-
t.integer "event_category_id"
87-
t.boolean "is_remind", default: false
88-
t.integer "remind_before_day", default: 1
89-
t.datetime "created_at", null: false
90-
t.datetime "updated_at", null: false
57+
t.integer "user_id", limit: 4
58+
t.integer "event_category_id", limit: 4
59+
t.boolean "is_remind", limit: 1, default: false
60+
t.integer "remind_before_day", limit: 4, default: 1
61+
t.datetime "created_at", null: false
62+
t.datetime "updated_at", null: false
9163
end
9264

9365
create_table "users", force: :cascade do |t|
94-
t.string "name"
95-
t.string "email"
96-
t.datetime "created_at", null: false
97-
t.datetime "updated_at", null: false
98-
t.string "password_digest"
99-
t.string "remember_digest"
100-
t.boolean "admin"
101-
t.string "activation_digest"
102-
t.boolean "activated"
66+
t.string "name", limit: 255
67+
t.string "email", limit: 255
68+
t.datetime "created_at", null: false
69+
t.datetime "updated_at", null: false
70+
t.string "password_digest", limit: 255
71+
t.string "remember_digest", limit: 255
72+
t.boolean "admin", limit: 1
73+
t.string "activation_digest", limit: 255
74+
t.boolean "activated", limit: 1
10375
t.datetime "activated_at"
104-
t.string "reset_digest"
76+
t.string "reset_digest", limit: 255
10577
t.datetime "reset_sent_at"
106-
t.integer "entrance_year"
78+
t.integer "entrance_year", limit: 4
10779
end
10880

109-
add_index "users", ["email"], name: "index_users_on_email", unique: true
81+
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
11082

11183
create_table "votes", force: :cascade do |t|
112-
t.integer "project_id"
113-
t.integer "project_option_id"
114-
t.integer "user_id"
115-
t.datetime "created_at", null: false
116-
t.datetime "updated_at", null: false
84+
t.integer "project_id", limit: 4
85+
t.integer "project_option_id", limit: 4
86+
t.integer "user_id", limit: 4
87+
t.datetime "created_at", null: false
88+
t.datetime "updated_at", null: false
11789
end
11890

11991
end

0 commit comments

Comments
 (0)
Please sign in to comment.