質問編集履歴
6
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,7 +100,7 @@
|
|
100
100
|
|
101
101
|
<% if Like.find_by(post_id: @post.id) %>
|
102
102
|
|
103
|
-
<%= link_to("/likes/#{@post.id}/destroy", method: :de
|
103
|
+
<%= link_to("/likes/#{@post.id}/destroy", method: :delete) do %>
|
104
104
|
|
105
105
|
<span class="fa fa-heart like-btn-unlike"></span>
|
106
106
|
|
5
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,13 +42,21 @@
|
|
42
42
|
|
43
43
|
post "/likes/:post_id/destroy", to: "likes#destroy"
|
44
44
|
|
45
|
-
|
45
|
+
get "/posts/:id", to: "posts#index"
|
46
|
+
|
47
|
+
|
48
|
+
|
46
|
-
|
49
|
+
resources :posts do
|
50
|
+
|
47
|
-
resources :
|
51
|
+
resources :likes, only: [:create, :destroy]
|
48
|
-
|
49
|
-
|
50
|
-
|
52
|
+
|
51
|
-
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
|
52
60
|
|
53
61
|
|
54
62
|
|
4
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -88,6 +88,8 @@
|
|
88
88
|
|
89
89
|
```
|
90
90
|
|
91
|
+
/likes/_like.html.erb
|
92
|
+
|
91
93
|
<% if Like.find_by(post_id: @post.id) %>
|
92
94
|
|
93
95
|
<%= link_to("/likes/#{@post.id}/destroy", method: :destroy) do %>
|
@@ -114,6 +116,8 @@
|
|
114
116
|
|
115
117
|
```
|
116
118
|
|
119
|
+
/posts/index.html.erb
|
120
|
+
|
117
121
|
<div class="vote-page">
|
118
122
|
|
119
123
|
<h1>投票する</h1>
|
3
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,12 +22,10 @@
|
|
22
22
|
|
23
23
|
### 該当のソースコード
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
```
|
28
|
-
|
29
25
|
ルーティング
|
30
26
|
|
27
|
+
```
|
28
|
+
|
31
29
|
Rails.application.routes.draw do
|
32
30
|
|
33
31
|
root 'static_pages#home'
|
@@ -56,10 +54,10 @@
|
|
56
54
|
|
57
55
|
```
|
58
56
|
|
59
|
-
```
|
60
|
-
|
61
57
|
モデル
|
62
58
|
|
59
|
+
```
|
60
|
+
|
63
61
|
class Like < ApplicationRecord
|
64
62
|
|
65
63
|
belongs_to :post
|
@@ -72,12 +70,24 @@
|
|
72
70
|
|
73
71
|
```
|
74
72
|
|
75
|
-
|
73
|
+
```
|
74
|
+
|
75
|
+
class Post < ApplicationRecord
|
76
|
+
|
77
|
+
validates :content, uniqueness: true, presence: true, length: { maximum: 30 }
|
78
|
+
|
79
|
+
has_many :likes, dependent: :destroy
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
end
|
76
84
|
|
77
85
|
```
|
78
86
|
|
79
87
|
ビュー
|
80
88
|
|
89
|
+
```
|
90
|
+
|
81
91
|
<% if Like.find_by(post_id: @post.id) %>
|
82
92
|
|
83
93
|
<%= link_to("/likes/#{@post.id}/destroy", method: :destroy) do %>
|
@@ -104,8 +114,38 @@
|
|
104
114
|
|
105
115
|
```
|
106
116
|
|
117
|
+
<div class="vote-page">
|
118
|
+
|
119
|
+
<h1>投票する</h1>
|
120
|
+
|
121
|
+
<ul class="list-unstyled">
|
122
|
+
|
123
|
+
<% @posts.each do |post| %>
|
124
|
+
|
125
|
+
<li>
|
126
|
+
|
127
|
+
<%= post.content %>
|
128
|
+
|
129
|
+
<%= render "likes/like", post: post %>
|
130
|
+
|
131
|
+
</li>
|
132
|
+
|
133
|
+
<% end %>
|
134
|
+
|
135
|
+
</ul>
|
136
|
+
|
137
|
+
<%= render 'posts/form' %>
|
138
|
+
|
139
|
+
</div>
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
|
144
|
+
|
107
145
|
コントローラー
|
108
146
|
|
147
|
+
```
|
148
|
+
|
109
149
|
class LikesController < ApplicationController
|
110
150
|
|
111
151
|
|
@@ -138,6 +178,76 @@
|
|
138
178
|
|
139
179
|
```
|
140
180
|
|
181
|
+
```
|
182
|
+
|
183
|
+
class PostsController < ApplicationController
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
def new
|
190
|
+
|
191
|
+
@post = Post.new
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
def create
|
198
|
+
|
199
|
+
@posts = Post.all
|
200
|
+
|
201
|
+
@post = Post.new(post_params)
|
202
|
+
|
203
|
+
if @post.save
|
204
|
+
|
205
|
+
flash[:success] = "登録されました"
|
206
|
+
|
207
|
+
redirect_to posts_url
|
208
|
+
|
209
|
+
else
|
210
|
+
|
211
|
+
render "index"
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
def index
|
220
|
+
|
221
|
+
@posts = Post.all
|
222
|
+
|
223
|
+
@post = Post.new
|
224
|
+
|
225
|
+
@like = Like.new
|
226
|
+
|
227
|
+
counts(@post)
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
private
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
def post_params
|
238
|
+
|
239
|
+
params.require(:post).permit(:content)
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
```
|
250
|
+
|
141
251
|
|
142
252
|
|
143
253
|
|
2
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -146,6 +146,6 @@
|
|
146
146
|
|
147
147
|
### 補足情報(FW/ツールのバージョンなど)
|
148
148
|
|
149
|
-
http://blog.livedoor.jp/tomoyayoshikawa1022/archives/3697356.html
|
149
|
+
[リンク内容](http://blog.livedoor.jp/tomoyayoshikawa1022/archives/3697356.html)
|
150
150
|
|
151
151
|
こちらのサイトを参考にさせてもらいました。というか、ほぼコピペ…
|
1
追加情報
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
railsでいいね機能
|
1
|
+
railsでpostにいいね機能を実装中
|
test
CHANGED
File without changes
|