質問編集履歴

4

写真の追記

2018/11/24 16:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -155,3 +155,7 @@
155
155
  <%= f.file_field :image_name %>
156
156
 
157
157
  ```
158
+
159
+
160
+
161
+ ![イメージ説明](3f14827b52e48e86ee8d901cf8c6748e.png)

3

コードの追記

2018/11/24 16:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,79 +6,17 @@
6
6
 
7
7
 
8
8
 
9
- ```ruby
9
+ ```HTML
10
10
 
11
- <!-- app/views/devise/registrations/edit.html.erb -->
11
+ <!-- index.html.erb ユーザー一覧ページ-->
12
12
 
13
- <html>
14
-
15
- <body>
16
-
17
- <div id="form">
18
-
19
- <p class="form-title">編集</p>
20
-
21
- <form action="post">
22
-
23
- <div class="field">
24
-
25
- <!--画像アップに関するコード -->
26
-
27
- <%= form_tag("/users/#{@user.id}/update", {multipart: true}) do %>
13
+ <img src="<%= "/user_images/#{user.image_name}" %>">
28
-
29
- <p><%= f.label :image %><br /></p>
30
-
31
- <%= f.file_field :image %>
32
-
33
- </div>
34
14
 
35
15
  ```
36
16
 
37
-
38
-
39
- ### 困っている事
40
-
41
- ![ユーザー編集](2657ad213379878d5bfff99049d86e0d.png)
42
-
43
- 写真のように、写真を追加する項目まで作成する事が出来ました。しかし、この写真を「編集完了」のボタンを押しても反映されなくて困っています。
44
-
45
- 考えてられる理由としては保存先がないか、マイグレーションファイルがないかによる原因かなとは思いつつもはっきりした理由がないのでどうしたらいいか分かりません。
46
-
47
-
48
-
49
- ### 追記
50
-
51
17
  ```ruby
52
18
 
53
- <!-- application_controller -->
19
+ //users_controller.rb
54
-
55
- class ApplicationController < ActionController::Base
56
-
57
- protect_from_forgery with: :exception
58
-
59
- before_action :authenticate_user!, only: [:/]
60
-
61
- before_action :configure_permitted_parameters, if: :devise_controller?
62
-
63
-
64
-
65
- protected
66
-
67
- def configure_permitted_parameters
68
-
69
- devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :image])
70
-
71
- devise_parameter_sanitizer.permit(:account_update, keys: [:name])
72
-
73
- end
74
-
75
- end
76
-
77
- ```
78
-
79
-
80
-
81
- ```ruby
82
20
 
83
21
  class UsersController < ApplicationController
84
22
 
@@ -108,11 +46,29 @@
108
46
 
109
47
  def create
110
48
 
111
- @user = User.new(name: params[:name])
49
+ @user = User.new(
112
50
 
113
- @user.save
51
+ name: params[:name],
114
52
 
53
+ email: params[:email],
54
+
55
+ image_name: "default_user.jpg",
56
+
57
+ )
58
+
59
+ if @user.save
60
+
61
+ session[:user_id] = @user.id
62
+
63
+ flash[:notice] = "ユーザー登録が完了しました"
64
+
115
- redirect_to("/users/#{@user.id}")
65
+ redirect_to("/users/#{@user.id}")
66
+
67
+ else
68
+
69
+ render("users/new")
70
+
71
+ end
116
72
 
117
73
  end
118
74
 
@@ -126,6 +82,76 @@
126
82
 
127
83
 
128
84
 
85
+ def update
86
+
87
+ @user = User.find_by(id: params[:id])
88
+
89
+ @user.name =params[:name]
90
+
91
+ @user.email = params[:email]
92
+
93
+
94
+
95
+ if params[:image]
96
+
97
+ @user.image_name = "#{@user.id}.jpg"
98
+
99
+ image = params[:image]
100
+
101
+ File.binwrite("public/user_images/#{@user.image_name}", image.read)
102
+
103
+ end
104
+
105
+ end
106
+
107
+
108
+
129
109
  end
130
110
 
131
111
  ```
112
+
113
+
114
+
115
+ ```ruby
116
+
117
+ //application.html.rb
118
+
119
+ class ApplicationController < ActionController::Base
120
+
121
+ protect_from_forgery with: :exception
122
+
123
+ before_action :authenticate_user!, only: [:/]
124
+
125
+ before_action :configure_permitted_parameters, if: :devise_controller?
126
+
127
+
128
+
129
+ protected
130
+
131
+ def configure_permitted_parameters
132
+
133
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :image])
134
+
135
+ devise_parameter_sanitizer.permit(:account_update, keys: [:name])
136
+
137
+ end
138
+
139
+ end
140
+
141
+
142
+
143
+ ```
144
+
145
+
146
+
147
+ ```HTML
148
+
149
+ <!-- app/views/devise/registrations/edit.html.erb -->
150
+
151
+ <%= form_tag("/users/#{@user.id}/update", {multipart: true}) do %>
152
+
153
+ <p><%= f.label :image_name %><br /></p>
154
+
155
+ <%= f.file_field :image_name %>
156
+
157
+ ```

2

コードの追記

2018/11/24 16:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -75,3 +75,57 @@
75
75
  end
76
76
 
77
77
  ```
78
+
79
+
80
+
81
+ ```ruby
82
+
83
+ class UsersController < ApplicationController
84
+
85
+ def index
86
+
87
+ @users = User.all
88
+
89
+ end
90
+
91
+
92
+
93
+ def show
94
+
95
+ @user = User.find_by(id: params[:id])
96
+
97
+ end
98
+
99
+
100
+
101
+ def new
102
+
103
+ @user = User.new
104
+
105
+ end
106
+
107
+
108
+
109
+ def create
110
+
111
+ @user = User.new(name: params[:name])
112
+
113
+ @user.save
114
+
115
+ redirect_to("/users/#{@user.id}")
116
+
117
+ end
118
+
119
+
120
+
121
+ def edit
122
+
123
+ @user = User.find_by(id: params[:id])
124
+
125
+ end
126
+
127
+
128
+
129
+ end
130
+
131
+ ```

1

コードの追記

2018/11/24 04:31

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,35 @@
43
43
  写真のように、写真を追加する項目まで作成する事が出来ました。しかし、この写真を「編集完了」のボタンを押しても反映されなくて困っています。
44
44
 
45
45
  考えてられる理由としては保存先がないか、マイグレーションファイルがないかによる原因かなとは思いつつもはっきりした理由がないのでどうしたらいいか分かりません。
46
+
47
+
48
+
49
+ ### 追記
50
+
51
+ ```ruby
52
+
53
+ <!-- application_controller -->
54
+
55
+ class ApplicationController < ActionController::Base
56
+
57
+ protect_from_forgery with: :exception
58
+
59
+ before_action :authenticate_user!, only: [:/]
60
+
61
+ before_action :configure_permitted_parameters, if: :devise_controller?
62
+
63
+
64
+
65
+ protected
66
+
67
+ def configure_permitted_parameters
68
+
69
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :image])
70
+
71
+ devise_parameter_sanitizer.permit(:account_update, keys: [:name])
72
+
73
+ end
74
+
75
+ end
76
+
77
+ ```