質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

381閲覧

いいね機能を実装しようとしたらNoMethodErrorが出てしまった。

kimura.t

総合スコア0

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/08/14 12:14

編集2021/08/14 19:56

前提・実現したいこと

Railsで写真投稿アプリを作っています
いいね機能を実装中に以下のエラーメッセージが発生しました。
難しいかもしれませんがお力を貸していただけると幸いです。
NoMethodError in Recipes#show

Showing /Users/projects/shoes-recipe/app/views/recipes/show.html.erb where line #8 raised:
undefined method `id' for nil:NilClass

発生している問題・エラーメッセージ

Extracted source (around line #8):   </p> <% if Like.find_by(user_id: @current_user.id, recipe_id: @recipe.id) %> **←赤字部分** <%= link_to("/likes/#{@recipe.id}/destroy", {method: "recipe"}) do %> <span class="fa fa-heart like-btn-unlike"></span> <% end %>

該当のソースコード

show.html.erb <main class="main"> <div class="inner"> <div class="recipe__wrapper"> <p class="recipe__hedding"> <%= @recipe.title %> </p> <% if Like.find_by(user_id: @current_user.id, recipe_id: @recipe.id) %> <%= link_to("/likes/#{@recipe.id}/destroy", {method: "recipe"}) do %> <span class="fa fa-heart like-btn-unlike"></span> <% end %> <% else %> <%= link_to("/likes/#{@recipe.id}/create", {method: "recipe"}) do %> <span class="fa fa-heart like-btn"></span> <% end %> <% end %> <%= @likes_count %> <%= link_to "by #{@recipe.user.name}", user_path(@recipe.user), class: :recipe__user %> <% if current_user == @recipe.user%> <div class="recipe__manage"> <%= link_to "編集する", edit_recipe_path(@recipe), class: :recipe__btn %> <%= link_to "削除する", recipe_path(@recipe), method: :delete, class: :recipe__btn %> </div> <% end %> <div class="recipe__image"> <%= image_tag @recipe.image %> </div> <div class="recipe__body"> <div class="recipe__detail"> <p class="detail__title">使った道具</p> <p class="detail__message"> <%= @recipe.cream %> </p> </div> <div class="recipe__detail"> <p class="detail__title">手入れの手順</p> <p class="detail__message"> <%= @recipe.procedure %> </p> </div> </div> <div class="recipe__comments"> <% if user_signed_in? %> <%= form_with model: [@recipe, @comment], local: true do |f|%> <div class="field"> <%= f.label :text, "コメント" %><br /> <%= f.text_field :text %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <% end %> <ul class="comments_lists"> <% @comments.each do |comment| %> <li class="comments_list"> <%= comment.text%> <%= link_to "(#{comment.user.name})", user_path(comment.user), class: :comment_user %> </li> <% end %> <ul> </div> </div> </div> </main>
likes_controller.rb class LikesController < ApplicationController before_action :authenticate_user def create @like = Like.new(user_id: @current_user.id, recipe_id: params[:recipe_id]) @like.save redirect_to("/recipes/#{params[:recipe_id]}") end def destroy @like = Like.find_by(user_id: @current_user.id, recipe_id: params[:recipe_id]) @like.destroy redirect_to("/recipes/#{params[:recipe_id]}") end end
routes.rb Rails.application.routes.draw do devise_for :users root to: "recipes#index" resources :recipes, only: [:new, :create, :show, :edit, :update, :destroy] do resources :comments, only: :create resources :likes, only: [:create, :destroy] end resources :users, only: :show end

試したこと

イメージ説明
ターミナルを見たところrecipeのidは取得できているのだが、userのidが取得できていないようです。
progateのやり方に沿って作業をしていたのだがどうしてもうまくいきません。
お力を貸していただけると幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

J_O

2021/08/18 14:03

ログイン機能はdeviseで実装してますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問