konchanxxx / menta

MENTAのタスク管理用リポジトリ
0 stars 0 forks source link

掲示板機能にいいね機能を実装したい #77

Open yuumasakura777 opened 5 years ago

yuumasakura777 commented 5 years ago

概要

現在作成中の掲示板機能にツイッターのようないいね機能(ハートマーク)をつけたい。

実現したいこと

ハートボタンを押せば赤く点灯しいいねができいいね数が増え、いいねしてあるボタンを押すと点灯がなくなり、いいね数が一つ減る機能を実装したいです。

大まかな流れと、私の制作物に合った作り方を教えていただきたいです。

困っていること

qiitaなどで調べながら実装しようと試みましたが、方法が微妙に違っており、どれを利用して作って行けば分からない状況です。

困っていることがバグの場合は事象やログ、エラーメッセージをできるだけそのまま(抜粋などしない)下記に記載してください。

解決するために行ったこと

問題となっている箇所の予想

問題となっているアプリケーションのGitHub URL

https://github.com/yuumasakura777/posted_function

konchanxxx commented 5 years ago

明日確認して回答します:bow:

konchanxxx commented 5 years ago

いいね機能を実装するならAjaxという技術を採用することになると思います。記事とユーザーを多対多で紐づけるような中間テーブルを作成して対応することになるかなと思います。不明点があれば追加で質問お願いします:bow:

https://qiita.com/jaramon/items/248bcb4b56e9fed8fc90

yuumasakura777 commented 5 years ago

リンク先の記事を見ながら実装していたのですが、Ajaxに入るまでの_like.html.erbを実装した所でエラーが出てしまいました。エラー内容は、 undefined method `current_user?' for #<#:0x00007ff8163e2fb8> Did you mean? current_use rcurrent_page? というもので、current_userが定義されていないというものなのですが、application.controllerで既に定義しているのでどうすればいいか分からなくなってしましました。 このエラーを解決するにはどうすればいいか教えてください? 追加したコードは以下のgithubに載せています。

https://github.com/yuumasakura777/posted_function

konchanxxx commented 5 years ago

def current_userは定義しているけど def current_user? (?ついてるやつ)は定義していない感じですかね?🤔

yuumasakura777 commented 5 years ago

def cuurent_user?の?を見落としていました。

cuurent_user?を定義したのですが、current_user?の引数はどうしたらいいですかね? そして処理はdef current_userと同じですかね?

konchanxxx commented 5 years ago

current_user?ってcurrent_userが存在したらtrueを返すみたいなメソッドですかね? でしたら引数はいらないかなと思っており、下記のようにcurrent_userを参照する形にすれば良いと思いますmm

def current_user?
  !!current_user
end

!! をつけることで真偽値が返ってくるようになります。 https://techracho.bpsinc.jp/hachi8833/2016_11_15/28993

yuumasakura777 commented 5 years ago

Koncahnさんのリンク先の実装ページを見ながらやっているのですが、コントローラーでcurrent_userというメソッドを定義している所がないので分からないです。

そして、 def current_user? !!current_user end をapplication_controllerで定義したのですが、定義したdef current_user?の部分でArgumentErrorが出てしまいました。

https://github.com/yuumasakura777/posted_function

konchanxxx commented 5 years ago

参考記事がRailsチュートリアルを通しでやっている感じなのでその前の章とかで実装しているかもですね🤔

konchanxxx commented 5 years ago

https://github.com/yuumasakura777/posted_function/blob/master/app/controllers/application_controller.rb#L19 はいいねの真偽値じゃなくてログインユーザーが存在しているかどうかだと思いますmm

konchanxxx commented 5 years ago

定義したdef current_user?の部分でArgumentErrorが出てしまいました。

これエラーをベタ貼りして欲しいですmm

yuumasakura777 commented 5 years ago

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 #<#:0x00007fe63cbbad80> Did you mean? @posts

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:inblock 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'

https://github.com/yuumasakura777/posted_function

konchanxxx commented 5 years ago

ありがとうございます:bow: undefined local variable or methodpost'` の部分はどう解釈している感じでしょう?

yuumasakura777 commented 5 years ago

postという変数が定義されていないということだと私は感じたのですが…

konchanxxx commented 5 years ago

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 } %>
konchanxxx commented 5 years ago

そのあとまた別のエラーが出てましたが🤔

konchanxxx commented 5 years ago

そのエラーの箇所に関連してきますが 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

としてますがこれだと

と言った問題点を含んでいます。

  def current_user
    @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
  end

  def current_user?
    !!current_user
  end

としてあげたほうがスマートかなと思います:bow:

yuumasakura777 commented 5 years ago

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'

というエラーが発生してしまいました。

konchanxxx commented 5 years ago

app/controllers/application_controller.rb:19:in current_user?' で ArgumentError が発生しているようなのでcurrent_user?の引数とかに問題があるかなと思ったのですがどうでしょう?

konchanxxx commented 5 years ago

@yuumasakura777