Open yuumasakura777 opened 5 years ago
明日確認して回答します:bow:
いいね機能を実装するならAjaxという技術を採用することになると思います。記事とユーザーを多対多で紐づけるような中間テーブルを作成して対応することになるかなと思います。不明点があれば追加で質問お願いします:bow:
リンク先の記事を見ながら実装していたのですが、Ajaxに入るまでの_like.html.erbを実装した所でエラーが出てしまいました。エラー内容は、
undefined method `current_user?' for #<#
def current_user
は定義しているけど def current_user?
(?ついてるやつ)は定義していない感じですかね?🤔
def cuurent_user?の?を見落としていました。
cuurent_user?を定義したのですが、current_user?の引数はどうしたらいいですかね? そして処理はdef current_userと同じですかね?
current_user?ってcurrent_userが存在したらtrueを返すみたいなメソッドですかね? でしたら引数はいらないかなと思っており、下記のようにcurrent_userを参照する形にすれば良いと思いますmm
def current_user?
!!current_user
end
!!
をつけることで真偽値が返ってくるようになります。
https://techracho.bpsinc.jp/hachi8833/2016_11_15/28993
Koncahnさんのリンク先の実装ページを見ながらやっているのですが、コントローラーでcurrent_userというメソッドを定義している所がないので分からないです。
そして、 def current_user? !!current_user end をapplication_controllerで定義したのですが、定義したdef current_user?の部分でArgumentErrorが出てしまいました。
参考記事がRailsチュートリアルを通しでやっている感じなのでその前の章とかで実装しているかもですね🤔
https://github.com/yuumasakura777/posted_function/blob/master/app/controllers/application_controller.rb#L19 はいいねの真偽値じゃなくてログインユーザーが存在しているかどうかだと思いますmm
定義したdef current_user?の部分でArgumentErrorが出てしまいました。
これエラーをベタ貼りして欲しいですmm
apllication_controllerにdef current_user?を定義した結果でたエラーが以下でです。 処理内容は、def current_userと同じです。
NameError in Posts#index
Showing /Users/takahashiyuuma/posted_function/app/views/likes/_like.html.erb where line #2 raised:
undefined local variable or method `post' for #<#
Trace of template inclusion: app/views/posts/index.html.erb
Rails.root: /Users/takahashiyuuma/posted_function
Application Trace | Framework Trace | Full Trace
app/views/likes/_like.html.erb:2:in _app_views_likes__like_html_erb__268309290480411415_70313382565440' app/views/posts/index.html.erb:13:in
block in _app_views_posts_index_html_erb__2911767763768846008_70313419070320'
app/views/posts/index.html.erb:6:in `_app_views_posts_index_html_erb__2911767763768846008_70313419070320'
ありがとうございます:bow:
undefined local variable or method
post'`
の部分はどう解釈している感じでしょう?
postという変数が定義されていないということだと私は感じたのですが…
postという変数が定義されていないということだと私は感じたのですが…
ですです。 そのためpostというパーシャルに渡したはずの変数がうまく渡っていない可能性があります。 問題の切り分けとしてはパーシャル部分に問題があるのか呼び出し元の方で問題があるのかという話になります。
pry
というデバッグツールがあるのでそれを使って変数が渡っているかどうか確認すると良いと思います。これはおすすめ本として記載させて頂いたプロになるための〜という本の中でも出てきます。
https://qiita.com/port-development/items/5ea6448eb2b45c70ef65
私の方でも見てみましたが、呼び出し元の@postsのループ内では正常にpostが定義されていてパーシャルの方では参照できなくなっていたので渡す際の定義に問題がありそうとあたりをつけました。
https://qiita.com/bambis13s/items/015f5bf921eb820ef652
https://github.com/yuumasakura777/posted_function/blob/master/app/views/posts/index.html.erb#L13
の箇所の記述がよくなかったみたいで、partialとlocals両方記述するか両方省略するかしてやるとここのエラーについてはうまく動作しました。
<%= render "likes/like", post: post %>
or
<%= render partial: "likes/like", locals: { post: post } %>
そのあとまた別のエラーが出てましたが🤔
そのエラーの箇所に関連してきますが https://github.com/yuumasakura777/posted_function/blob/master/app/controllers/application_controller.rb#L15-L21 のコードはあまり宜しくないですね。
def current_user
@current_user ||=User.find_by(id: session[:user_id]) if session[:user_id]
end
def current_user?
@current_user ||=User.find_by(id: session[:user_id]) if session[:user_id]
end
としてますがこれだと
?
は基本的にtrue or falseを返すことを期待するので反していると言った問題点を含んでいます。
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
def current_user?
!!current_user
end
としてあげたほうがスマートかなと思います:bow:
Konchanさんの仰ったように直すとパーシャルの所は動くようになりました。 しかし新たに定義したdef current_user?の所で
ArgumentError in Posts#index
Trace of template inclusion: app/views/posts/index.html.erb
Rails.root: /Users/takahashiyuuma/posted_function
Application Trace | Framework Trace | Full Trace
app/controllers/application_controller.rb:19:in current_user?' app/views/likes/_like.html.erb:2:in
_app_views_likeslike_html_erb4241132884058978468_70156926939360'
app/views/posts/index.html.erb:13:in block in _app_views_posts_index_html_erb__2710445260554737309_70156936072340' app/views/posts/index.html.erb:6:in
_app_views_posts_index_html_erb__2710445260554737309_70156936072340'
というエラーが発生してしまいました。
app/controllers/application_controller.rb:19:in current_user?' で ArgumentError が発生しているようなのでcurrent_user?の引数とかに問題があるかなと思ったのですがどうでしょう?
@yuumasakura777
概要
現在作成中の掲示板機能にツイッターのようないいね機能(ハートマーク)をつけたい。
実現したいこと
ハートボタンを押せば赤く点灯しいいねができいいね数が増え、いいねしてあるボタンを押すと点灯がなくなり、いいね数が一つ減る機能を実装したいです。
大まかな流れと、私の制作物に合った作り方を教えていただきたいです。
困っていること
qiitaなどで調べながら実装しようと試みましたが、方法が微妙に違っており、どれを利用して作って行けば分からない状況です。
困っていることがバグの場合は事象やログ、エラーメッセージをできるだけそのまま(抜粋などしない)下記に記載してください。
解決するために行ったこと
問題となっている箇所の予想
問題となっているアプリケーションのGitHub URL
https://github.com/yuumasakura777/posted_function