質問編集履歴

2

viewの追加

2019/11/01 23:16

投稿

sn.jr
sn.jr

スコア53

test CHANGED
File without changes
test CHANGED
@@ -281,3 +281,71 @@
281
281
  </div>
282
282
 
283
283
  ```
284
+
285
+ ```
286
+
287
+ <div class="container">
288
+
289
+ <%=form_tag("/users/#{@user.id}/update",{multipart: true}) do%>
290
+
291
+ <div class="form">
292
+
293
+ <h1>編集する</h1>
294
+
295
+ <div class="form-input">
296
+
297
+ <label for="frame">Your Name</label>
298
+
299
+ <input id="frame" name="name" class="Full_name" type="text" value="<%=@user.name %>">
300
+
301
+ </div>
302
+
303
+ <div class="form-input">
304
+
305
+ <label for="erame">Email</label>
306
+
307
+ <input id="erame" class="email" name="email" type="email" value="<%=@user.email %>">
308
+
309
+ </div>
310
+
311
+ <div class="form-input">
312
+
313
+ <label for="s-rame">学校</label>
314
+
315
+ <input id="s-rame" class="school" name="school" type="text" value="<%=@user.school %>">
316
+
317
+ </div>
318
+
319
+ <div class="form-input">
320
+
321
+ <label for="j-rame">職業経歴</label>
322
+
323
+ <textarea id="j-rame" class="jobs" name="jobs" type="text" ><%=@user.jobs %></textarea>
324
+
325
+ </div>
326
+
327
+ <div class="form-input">
328
+
329
+ <label for="in-rame">自己紹介</label>
330
+
331
+ <textarea id="in-rame" class="intro" name="intro" type="text"><%=@user.intro %></textarea>
332
+
333
+ </div>
334
+
335
+ <div class="form-input">
336
+
337
+ <label for="prame">Your Photo</label>
338
+
339
+ <input id="prame" class= "photo" name="image" type="file" value="<%=@user.image_name %>">
340
+
341
+ </div>
342
+
343
+ <input type="submit" value="保存">
344
+
345
+ </div>
346
+
347
+ <% end %>
348
+
349
+ </div>
350
+
351
+ ```

1

controllerとviewの追加

2019/11/01 23:16

投稿

sn.jr
sn.jr

スコア53

test CHANGED
@@ -1 +1 @@
1
- inputで入力データを送信するときname="カラム名"だと思うのですが、データがstringではなくtextの場合はどうすればよしょうか?
1
+ カラムタイプがtextのものを送信して、表示させたいで
test CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
 
4
4
 
5
- カラムタイプがtextのものを送信して、表示させたいのですがまく表示されません。
5
+ inputで入力データを送信するときname="カラム名"だと思うのですが、データがstringではなくtextの場合はどうすればよいのでしょか?
6
+
7
+
6
8
 
7
9
 
8
10
 
@@ -39,3 +41,243 @@
39
41
  ```
40
42
 
41
43
  この場合最初のintro以外は表示させることができるのですがカラムタイプがtextのintroだけ表示させることができません。textをstringにさせたらできます。
44
+
45
+ ```
46
+
47
+ class UsersController < ApplicationController
48
+
49
+ before_action :ensure_correct_user, {only: [:edit, :update]}
50
+
51
+ before_action :authenticate_user, {only: [:show, :index,:edit, :update]}
52
+
53
+ def index
54
+
55
+ @users =User.all
56
+
57
+ end
58
+
59
+
60
+
61
+ def create
62
+
63
+ @user = User.new(name: params[:name], email: params[:email], password: params[:password],image_name: "default_user.jpg")
64
+
65
+ if @user.save
66
+
67
+ redirect_to("/users/#{@user.id}")
68
+
69
+ session[:user_id]= @user.id
70
+
71
+ else
72
+
73
+ render("/users/new")
74
+
75
+
76
+
77
+ end
78
+
79
+ end
80
+
81
+
82
+
83
+ def show
84
+
85
+
86
+
87
+ @user = User.find_by(id: params[:id])
88
+
89
+
90
+
91
+ end
92
+
93
+ def new
94
+
95
+ @user = User.new
96
+
97
+ end
98
+
99
+ def edit
100
+
101
+ @user = User.find_by(id: params[:id])
102
+
103
+ end
104
+
105
+ def update
106
+
107
+ @user = User.find_by(id: params[:id])
108
+
109
+ @user.name = params[:name]
110
+
111
+ @user.email = params[:email]
112
+
113
+ @user.intro = params[:intro]
114
+
115
+ @user.school = params[:school]
116
+
117
+ @user.jobs = params[:jobs]
118
+
119
+
120
+
121
+ if params[:image]
122
+
123
+ @user.image_name="#{@user.id}.jpg"
124
+
125
+ image =params[:image]
126
+
127
+ File.binwrite("public/user_images/#{@user.image_name}", image.read)
128
+
129
+ end
130
+
131
+ if @user.save
132
+
133
+ redirect_to("/users/#{@user.id}")
134
+
135
+ else
136
+
137
+ render("users/edit")
138
+
139
+ end
140
+
141
+ end
142
+
143
+
144
+
145
+ def login_form
146
+
147
+ password = params[:password]
148
+
149
+ end
150
+
151
+
152
+
153
+ def login
154
+
155
+ @user = User.find_by(email: params[:email], password: params[:password])
156
+
157
+ if @user
158
+
159
+ session[:user_id]=@user.id
160
+
161
+ flash[:notice]="ログインしました"
162
+
163
+ redirect_to("/posts/index")
164
+
165
+ else
166
+
167
+ @error_message ="メールアドレスまたはパスワードが間違っています"
168
+
169
+ @email = params[:email]
170
+
171
+ @password = params[:password]
172
+
173
+ render("users/login_form")
174
+
175
+ end
176
+
177
+
178
+
179
+ end
180
+
181
+
182
+
183
+ def logout
184
+
185
+
186
+
187
+ session[:user_id]= nil
188
+
189
+ flash[:notice]="ログアウトしました"
190
+
191
+ redirect_to ("/login")
192
+
193
+ end
194
+
195
+
196
+
197
+ def ensure_correct_user
198
+
199
+ if @current_user.id != params[:id].to_i
200
+
201
+ flash[:notice]="権限がありません"
202
+
203
+ redirect_to("/users/index")
204
+
205
+
206
+
207
+
208
+
209
+ end
210
+
211
+ end
212
+
213
+ private
214
+
215
+ def user_params
216
+
217
+ params.require(:user).permit(:image, :x, :y, :width, :height)
218
+
219
+ end
220
+
221
+
222
+
223
+ end
224
+
225
+
226
+
227
+ ```
228
+
229
+ ```
230
+
231
+
232
+
233
+
234
+
235
+ <div class="container">
236
+
237
+ <div class="profile-card">
238
+
239
+ <div class="profile-card__inner">
240
+
241
+ <div class="profile-thumb">
242
+
243
+ <img src="<%="/user_images/#{@user.image_name}" %>" class="user-image" alt="アイコン">
244
+
245
+
246
+
247
+ </div>
248
+
249
+ <div class="profile-content">
250
+
251
+ <span class="profile-name"><%=@user.name%></span>
252
+
253
+ <span class="profile-job"><%=@user.email %></span>
254
+
255
+ <span class="profile-job"><%=@user.school %></span>
256
+
257
+ <span class="job-intro"><%=@user.jobs%></span>
258
+
259
+ <span class="profile-intro"><%=@user.intro %></span>
260
+
261
+ <%=link_to("編集する","/users/#{@user.id}/edit") %>
262
+
263
+ </div>
264
+
265
+ </div>
266
+
267
+ <div class="profile-sns">
268
+
269
+ <span>FOLLOW ME</span>
270
+
271
+ <a href="#"><i class="fab fa-twitter-square"></i></a>
272
+
273
+ <a href="#"><i class="fab fa-instagram"></i></a>
274
+
275
+ <a href="#"><i class="fas fa-envelope-square"></i></a>
276
+
277
+ </div>
278
+
279
+ </div>
280
+
281
+ </div>
282
+
283
+ ```