質問編集履歴
5
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,7 +124,7 @@
|
|
124
124
|
|
125
125
|
ActionView::Template::Error (Missing partial likes/_like, application/_like with {:locale=>[:ja], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
|
126
126
|
|
127
|
-
* "/Users/
|
127
|
+
* "/Users/TOM/myapp/app/views"
|
128
128
|
|
129
129
|
):
|
130
130
|
|
4
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -122,31 +122,17 @@
|
|
122
122
|
|
123
123
|
```error
|
124
124
|
|
125
|
-
Action
|
125
|
+
ActionView::Template::Error (Missing partial likes/_like, application/_like with {:locale=>[:ja], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
|
126
126
|
|
127
|
-
|
127
|
+
* "/Users/SHIGE/mymeal3ajax/app/views"
|
128
128
|
|
129
|
-
|
129
|
+
):
|
130
130
|
|
131
|
-
|
131
|
+
1: $("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
132
132
|
|
133
|
-
|
133
|
+
|
134
134
|
|
135
|
-
Prefix Verb URI Pattern Controller#Action
|
136
|
-
|
137
|
-
|
135
|
+
app/views/likes/create.js.erb:1:in `_app_views_likes_create_js_erb__364044909247640962_70328458376380'
|
138
|
-
|
139
|
-
POST /likes/:post_id/destroy(.:format) likes#destroy
|
140
|
-
|
141
|
-
```
|
142
|
-
|
143
|
-
```Model
|
144
|
-
|
145
|
-
class LikesController < ApplicationController
|
146
|
-
|
147
|
-
before_action :authenticate_user
|
148
|
-
|
149
|
-
belongs_to :post
|
150
136
|
|
151
137
|
```
|
152
138
|
|
3
コード追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -114,4 +114,40 @@
|
|
114
114
|
|
115
115
|
jsは全くいじってこなかったのですが、初心者でも作れるいいね!機能のやり方を教えてください。
|
116
116
|
|
117
|
+
|
118
|
+
|
119
|
+
追記:
|
120
|
+
|
121
|
+
実際にいいねボタンを押すと、このようなエラーがコンソールで出ています。
|
122
|
+
|
123
|
+
```error
|
124
|
+
|
125
|
+
ActionController::RoutingError (undefined method `belongs_to' for LikesController:Class):
|
126
|
+
|
127
|
+
app/controllers/likes_controller.rb:4:in `<class:LikesController>'
|
128
|
+
|
129
|
+
app/controllers/likes_controller.rb:1:in `<main>'
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
```Route
|
134
|
+
|
135
|
+
Prefix Verb URI Pattern Controller#Action
|
136
|
+
|
137
|
+
POST /likes/:post_id/create(.:format) likes#create
|
138
|
+
|
139
|
+
POST /likes/:post_id/destroy(.:format) likes#destroy
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
```Model
|
144
|
+
|
145
|
+
class LikesController < ApplicationController
|
146
|
+
|
147
|
+
before_action :authenticate_user
|
148
|
+
|
149
|
+
belongs_to :post
|
150
|
+
|
151
|
+
```
|
152
|
+
|
117
153
|
よろしくお願いします。
|
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,7 +100,7 @@
|
|
100
100
|
|
101
101
|
```js
|
102
102
|
|
103
|
-
ce
|
103
|
+
create.js.erb
|
104
104
|
|
105
105
|
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
106
106
|
|
1
コード追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,6 +18,100 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
+
```Model
|
22
|
+
|
23
|
+
class Like < ApplicationRecord
|
24
|
+
|
25
|
+
belongs_to :user
|
26
|
+
|
27
|
+
belongs_to :post, counter_cache: :likes_count
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
```Controller
|
34
|
+
|
35
|
+
class PostsController < ApplicationController
|
36
|
+
|
37
|
+
def index
|
38
|
+
|
39
|
+
@posts = Post.all.order(created_at: :desc)
|
40
|
+
|
41
|
+
@post = Post.find_by(id: params[:id])
|
42
|
+
|
43
|
+
@likes_count = Like.where(post_id: @post).count
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
class LikesController < ApplicationController
|
48
|
+
|
49
|
+
def create
|
50
|
+
|
51
|
+
@like = Like.create(user_id: @current_user.id, post_id: params[:post_id])
|
52
|
+
|
53
|
+
@likes = Like.where(post_id: params[:post_id])
|
54
|
+
|
55
|
+
@posts = Post.all
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def destroy
|
60
|
+
|
61
|
+
@like = Like.find_by(user_id: @current_user.id, post_id: params[:post_id])
|
62
|
+
|
63
|
+
@like.destroy
|
64
|
+
|
65
|
+
@likes = Like.where(post_id: params[:post_id])
|
66
|
+
|
67
|
+
@posts = Post.all
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
```View
|
76
|
+
|
77
|
+
<% if Like.find_by(user_id: @current_user.id, post_id: post.id) %>
|
78
|
+
|
79
|
+
<%= link_to("/likes/#{post.id}/destroy", :method => :post, id: "like-button", remote: true ) do %>
|
80
|
+
|
81
|
+
<span class="fa fa-heart fa-2x like-btn-unlike"></span>
|
82
|
+
|
83
|
+
<span><%= @likes_count %></span>
|
84
|
+
|
85
|
+
<% end %>
|
86
|
+
|
87
|
+
<% else %>
|
88
|
+
|
89
|
+
<%= link_to("/likes/#{post.id}/create", :method => :post, id: "like-button", remote: true) do %>
|
90
|
+
|
91
|
+
<span class="fa fa-heart fa-2x like-btn"></span>
|
92
|
+
|
93
|
+
<span><%= @likes_count %></span>
|
94
|
+
|
95
|
+
<% end %>
|
96
|
+
|
97
|
+
<% end %>
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
```js
|
102
|
+
|
103
|
+
cerate.js.erb
|
104
|
+
|
105
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
106
|
+
|
107
|
+
destroy.js.erb
|
108
|
+
|
109
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes }) %>");
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
jsファイルはview/likesに保存されています。
|
114
|
+
|
21
115
|
jsは全くいじってこなかったのですが、初心者でも作れるいいね!機能のやり方を教えてください。
|
22
116
|
|
23
117
|
よろしくお願いします。
|