質問編集履歴
4
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -68,6 +68,14 @@
|
|
68
68
|
---
|
69
69
|
以下のコードでmd5でハッシュ化したidの上8桁の取得が完了しました!!
|
70
70
|
文法ミスあれば教えていただきたいです!
|
71
|
+
|
72
|
+
▼投稿者のID
|
71
73
|
```
|
72
74
|
<%= Digest::MD5.hexdigest("request.remote_ip.to_s + #{@post.created_at.strftime("%Y%m%d")} + #{@post.id}")[0,8] %>
|
75
|
+
```
|
76
|
+
|
77
|
+
▼コメンターのID
|
78
|
+
```
|
79
|
+
名前:<%= comment.commenter %> <%= comment.created_at.strftime("%y/%m/%d(#{%w(日 月 火 水 木 金 土)[comment.created_at.wday]}) %H:%M:%S") %> ID:<%= Digest::MD5.hexdigest("request.remote_ip.to_s + #{comment.created_at.strftime("%Y%m%d")} + #{@post.id}")[0,8] %>
|
80
|
+
<%= simple_format comment.body %>
|
73
81
|
```
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -62,4 +62,12 @@
|
|
62
62
|
▼commentテーブルのカラムです
|
63
63
|
```
|
64
64
|
=> Comment(id: integer, commenter: string, body: text, post_id: integer, created_at: datetime, updated_at: datetime, ip: string)
|
65
|
+
```
|
66
|
+
|
67
|
+
<追記 2016.01.21>
|
68
|
+
---
|
69
|
+
以下のコードでmd5でハッシュ化したidの上8桁の取得が完了しました!!
|
70
|
+
文法ミスあれば教えていただきたいです!
|
71
|
+
```
|
72
|
+
<%= Digest::MD5.hexdigest("request.remote_ip.to_s + #{@post.created_at.strftime("%Y%m%d")} + #{@post.id}")[0,8] %>
|
65
73
|
```
|
2
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,9 +12,6 @@
|
|
12
12
|
Comment投稿者のIDを取得することができない。
|
13
13
|
コメントはsaveされるのだが、ipカラムにコメント投稿者のipが入っていない状態。
|
14
14
|
|
15
|
-
・実装したいこと
|
16
|
-
コメント投稿者の
|
17
|
-
|
18
15
|
以下が該当と予想されるコードです。
|
19
16
|
▼commnets_controller.rb
|
20
17
|
```
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,68 @@
|
|
1
1
|
rails初心者です。
|
2
2
|
|
3
3
|
現在練習で簡単な掲示板を製作しています。
|
4
|
-
投稿者のIPアドレスを取得したいのですが、今風のおすすめの方法など教えて頂きたいです。
|
4
|
+
投稿者のIPアドレスを取得したいのですが、今風のおすすめの方法など教えて頂きたいです。
|
5
|
+
|
6
|
+
|
7
|
+
==========
|
8
|
+
<追記>
|
9
|
+
Postにcommentを紐づけた実装をしています。
|
10
|
+
・問題
|
11
|
+
Post投稿者のIDは取得でき、データーベースに保存できたのだが、
|
12
|
+
Comment投稿者のIDを取得することができない。
|
13
|
+
コメントはsaveされるのだが、ipカラムにコメント投稿者のipが入っていない状態。
|
14
|
+
|
15
|
+
・実装したいこと
|
16
|
+
コメント投稿者の
|
17
|
+
|
18
|
+
以下が該当と予想されるコードです。
|
19
|
+
▼commnets_controller.rb
|
20
|
+
```
|
21
|
+
class CommentsController < ApplicationController
|
22
|
+
def create
|
23
|
+
@post = Post.find(params[:post_id])
|
24
|
+
|
25
|
+
@comment = @post.comments.create(comment_params)
|
26
|
+
@comment.ip = request.remote_ip
|
27
|
+
p request.remote_ip
|
28
|
+
p @comment.ip
|
29
|
+
|
30
|
+
respond_to do |format|
|
31
|
+
if @comment.valid?
|
32
|
+
format.html { redirect_to post_path(@post), notice: 'コメントが完了しました!!' }
|
33
|
+
else
|
34
|
+
format.html { redirect_to post_path(@post), notice: 'コメントに失敗しました。ニックネームとコメントを入れてください。' }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def comment_params
|
41
|
+
params.require(:comment).permit(:commenter, :body)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
▼_form.html.erb(コメントの投稿フォームです。postのshow.html.erbに紐付けられています。)
|
47
|
+
```
|
48
|
+
<%= form_for( [@post, @post.comments.build] ) do |f| %>
|
49
|
+
|
50
|
+
<div class="field">
|
51
|
+
<%= f.label :commenter, "ニックネーム" %>
|
52
|
+
<%= f.text_field :commenter,placeholder: :"名無しさん" %>
|
53
|
+
</div>
|
54
|
+
<div class="field">
|
55
|
+
<%= f.label :body, "コメント" %>
|
56
|
+
<%= f.text_area :body, cols: 60, rows: 3 %>
|
57
|
+
</div>
|
58
|
+
|
59
|
+
<div class="actions">
|
60
|
+
<%= f.submit "コメント投稿" %>
|
61
|
+
</div>
|
62
|
+
<% end %>
|
63
|
+
```
|
64
|
+
|
65
|
+
▼commentテーブルのカラムです
|
66
|
+
```
|
67
|
+
=> Comment(id: integer, commenter: string, body: text, post_id: integer, created_at: datetime, updated_at: datetime, ip: string)
|
68
|
+
```
|