質問編集履歴
5
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -61,7 +61,7 @@
|
|
61
61
|
実際にいいねボタンを押すと、このようなエラーがコンソールで出ています。
|
62
62
|
```error
|
63
63
|
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:
|
64
|
-
* "/Users/
|
64
|
+
* "/Users/TOM/myapp/app/views"
|
65
65
|
):
|
66
66
|
1: $("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
67
67
|
|
4
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -60,18 +60,11 @@
|
|
60
60
|
追記:
|
61
61
|
実際にいいねボタンを押すと、このようなエラーがコンソールで出ています。
|
62
62
|
```error
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
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:
|
64
|
+
* "/Users/SHIGE/mymeal3ajax/app/views"
|
65
|
+
):
|
66
|
+
1: $("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
67
|
+
|
68
|
+
app/views/likes/create.js.erb:1:in `_app_views_likes_create_js_erb__364044909247640962_70328458376380'
|
66
69
|
```
|
67
|
-
```Route
|
68
|
-
Prefix Verb URI Pattern Controller#Action
|
69
|
-
POST /likes/:post_id/create(.:format) likes#create
|
70
|
-
POST /likes/:post_id/destroy(.:format) likes#destroy
|
71
|
-
```
|
72
|
-
```Model
|
73
|
-
class LikesController < ApplicationController
|
74
|
-
before_action :authenticate_user
|
75
|
-
belongs_to :post
|
76
|
-
```
|
77
70
|
よろしくお願いします。
|
3
コード追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -56,4 +56,22 @@
|
|
56
56
|
```
|
57
57
|
jsファイルはview/likesに保存されています。
|
58
58
|
jsは全くいじってこなかったのですが、初心者でも作れるいいね!機能のやり方を教えてください。
|
59
|
+
|
60
|
+
追記:
|
61
|
+
実際にいいねボタンを押すと、このようなエラーがコンソールで出ています。
|
62
|
+
```error
|
63
|
+
ActionController::RoutingError (undefined method `belongs_to' for LikesController:Class):
|
64
|
+
app/controllers/likes_controller.rb:4:in `<class:LikesController>'
|
65
|
+
app/controllers/likes_controller.rb:1:in `<main>'
|
66
|
+
```
|
67
|
+
```Route
|
68
|
+
Prefix Verb URI Pattern Controller#Action
|
69
|
+
POST /likes/:post_id/create(.:format) likes#create
|
70
|
+
POST /likes/:post_id/destroy(.:format) likes#destroy
|
71
|
+
```
|
72
|
+
```Model
|
73
|
+
class LikesController < ApplicationController
|
74
|
+
before_action :authenticate_user
|
75
|
+
belongs_to :post
|
76
|
+
```
|
59
77
|
よろしくお願いします。
|
2
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -49,7 +49,7 @@
|
|
49
49
|
<% end %>
|
50
50
|
```
|
51
51
|
```js
|
52
|
-
|
52
|
+
create.js.erb
|
53
53
|
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
54
54
|
destroy.js.erb
|
55
55
|
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes }) %>");
|
1
コード追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,5 +8,52 @@
|
|
8
8
|
また他のサイトに記載のコードでも試したのですが、うまく行っていません。
|
9
9
|
jsを取り入れる前にRailsでは何か設定しないといけないとかあるのでしょうか。
|
10
10
|
|
11
|
+
```Model
|
12
|
+
class Like < ApplicationRecord
|
13
|
+
belongs_to :user
|
14
|
+
belongs_to :post, counter_cache: :likes_count
|
15
|
+
end
|
16
|
+
```
|
17
|
+
```Controller
|
18
|
+
class PostsController < ApplicationController
|
19
|
+
def index
|
20
|
+
@posts = Post.all.order(created_at: :desc)
|
21
|
+
@post = Post.find_by(id: params[:id])
|
22
|
+
@likes_count = Like.where(post_id: @post).count
|
23
|
+
end
|
24
|
+
class LikesController < ApplicationController
|
25
|
+
def create
|
26
|
+
@like = Like.create(user_id: @current_user.id, post_id: params[:post_id])
|
27
|
+
@likes = Like.where(post_id: params[:post_id])
|
28
|
+
@posts = Post.all
|
29
|
+
end
|
30
|
+
def destroy
|
31
|
+
@like = Like.find_by(user_id: @current_user.id, post_id: params[:post_id])
|
32
|
+
@like.destroy
|
33
|
+
@likes = Like.where(post_id: params[:post_id])
|
34
|
+
@posts = Post.all
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
```View
|
39
|
+
<% if Like.find_by(user_id: @current_user.id, post_id: post.id) %>
|
40
|
+
<%= link_to("/likes/#{post.id}/destroy", :method => :post, id: "like-button", remote: true ) do %>
|
41
|
+
<span class="fa fa-heart fa-2x like-btn-unlike"></span>
|
42
|
+
<span><%= @likes_count %></span>
|
43
|
+
<% end %>
|
44
|
+
<% else %>
|
45
|
+
<%= link_to("/likes/#{post.id}/create", :method => :post, id: "like-button", remote: true) do %>
|
46
|
+
<span class="fa fa-heart fa-2x like-btn"></span>
|
47
|
+
<span><%= @likes_count %></span>
|
48
|
+
<% end %>
|
49
|
+
<% end %>
|
50
|
+
```
|
51
|
+
```js
|
52
|
+
cerate.js.erb
|
53
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like}) %>")
|
54
|
+
destroy.js.erb
|
55
|
+
$("#like-buttons").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes }) %>");
|
56
|
+
```
|
57
|
+
jsファイルはview/likesに保存されています。
|
11
58
|
jsは全くいじってこなかったのですが、初心者でも作れるいいね!機能のやり方を教えてください。
|
12
59
|
よろしくお願いします。
|