前提・実現したいこと
Ruby on rails でtwitterのようなサービスのいいね機能を作っています。[参考にした記事です]
(http://qiita.com/nojinoji/items/2c66499848d882c31ffa)
初心者のため、何が間違っているのかさっぱりわかりません。ご指摘いただけると幸いです。
いいねされた数をtweets/index.html.erbに表示する際に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
NameError in Tweets#index
Showing C:/Users/kayo/Desktop/negapositter/app/views/tweets/index.html.erb where line #22 raised:
undefined local variable or method `tweet' for #<#Class:0x000000077ccac0:0x00000009e3b508>
Did you mean? tweet_url
@tweets
Extracted source (around line #22):
20
21
22
23
24
25
<%= t.created_at %> <br> (<%= tweet.liked_users.count %>) <%= link_to "ポジティブ変換する", tweet_path(t.id) %> <%= link_to "編集する", edit_tweet_path(t.id) %> <%= link_to "削除する", tweet_path(t.id), method: :delete %>
Rails.root: C:/Users/kayo/Desktop/negapositter
Application Trace | Framework Trace | Full Trace
app/views/tweets/index.html.erb:22:in block in _app_views_tweets_index_html_erb___107725008_82960300' app/views/tweets/index.html.erb:15:in
_app_views_tweets_index_html_erb___107725008_82960300'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
views/tweets.index.html.erb
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ネガポジったー</title> </head> <div class="p-3 mb-2 bg-light text-dark"> <body> <div class="container text-center"> <%= link_to "マイページへ", user_path(current_user.id) %> <a class="btn btn-primary" href="/tweets/new">ネガポジ変換してほしい悩みを投稿する</a> <br> <% @tweets.each do |t| %> <div> <%= t.body %> <br> <%= t.user.email %> <%= t.created_at %> <br> (<%= tweet.liked_users.count %>) <%= link_to "ポジティブ変換する", tweet_path(t.id) %> <%= link_to "編集する", edit_tweet_path(t.id) %> <%= link_to "削除する", tweet_path(t.id), method: :delete %> </div> <br> <% end %> <%= link_to 'ログアウト', destroy_user_session_path, method: :delete %> </div> </body> </div> </html>
###likes_controller.rb
class LikesController < ApplicationController def create @like = current_user.likes.create(tweet_id: params[:tweet_id]) redirect_back(fallback_location: root_path) end def destroy @like = Like.find_by(tweet_id: params[:tweet_id], user_id: current_user.id) @like.destroy redirect_back(fallback_location: root_path) end end
###like.rb
class Like < ApplicationRecord belongs_to :tweet belongs_to :user validates_uniqueness_of :tweet_id, scope: :user_id end
###tweets.controller.rb
class TweetsController < ApplicationController #加える行始まり #ログインしていなかったら、このコントローラーの全ての機能を使えない before_action :authenticate_user! #加える行終わり #全ての投稿を表示する画面 def index # Orderで並び替え @tweets = Tweet.all.order("id DESC") end #新しいTweetを投稿する時に使う def new @tweet = Tweet.new end #新しいTweetの保存 def create @tweet = Tweet.new(tweet_params) #現在ログインしているUserのidをTweetのuser_idという部分にセット @tweet.user_id = current_user.id #新しいTweetの保存に成功した場合 if @tweet.save #index.html.erbにページが移る redirect_to action: "index" #新しいTweetsの保存に失敗した場合 else #もう一回投稿画面へ redirect_to action: "new" end end #投稿の詳細表示 def show @tweet = Tweet.find(params[:id]) @like = Like.new @comments = @tweet.comments @comment = Comment.new end #投稿の編集 def edit @tweet = Tweet.find(params[:id]) end #投稿の削除 def destroy Tweet.find(params[:id]).destroy redirect_to action: :index end #アップデート def update @tweet = Tweet.find(params[:id]) if @tweet.update(tweet_params) redirect_to :action => "show", :id => @tweet.id else redirect_to :action => "new" end end def search #Viewのformで取得したパラメータをモデルに渡す @tweets = Tweet.search(params[:search]) end private #セキュリティのため、許可した:bodyというデータだけ取ってくるようにする def tweet_params params.require(:tweet).permit(:body) end end
試したこと
エラー部分をtweetからtweetsにしたり、likedをlikeにしたりしました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/22 23:44