前提・実現したいこと
ツイート投稿アプリをつくっています。
acts-as-taggable-onと言うgemを使い、ツイートのタグ付けを実装しました。
tag_cloudで、タグ一覧を表示させたいのですが、リファレンス通りにやってもエラーが出ました。
助言をいただけると幸いです
発生している問題・エラーメッセージ
NoMethodError in Tweets#index Showing /Users/fujiikayo/Desktop/sharefarm/app/views/tweets/index.html.erb where line #2 raised: undefined method `empty?' for nil:NilClass Extracted source (around line #2): <body> <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %> <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %> <% end %> <div class="container text-center"> <div class="row my-4">
該当のソースコード
tweet/index.html.erb
ruby
1<body> 2 <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %> 3 <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %> 4 <% end %> 5 <div class="container text-center"> 6 <div class="row my-4"> 7 <div class="col-sm-1"> 8 </div> 9 10 <div class="card cont col-sm-10 p-2"> 11 <strong>果物を検索する</strong> 12 <%= form_tag(controller:"tweets",action:"index") do %> 13 <%= text_field_tag :search %> 14 <%= submit_tag '検索',:name_list => nil,:address => nil, class:"btn-gradient-radius" %> 15 <% end %> 16 </div> 17#省略 18 19 <%= link_to t.user.name, user_path(t.user.id), class: "link_option" %> 20 </td> 21 </tr> 22 <tr> 23 <th>生産地</th> 24 <td> <%= t.address %></td> 25 </tr> 26 <tr> 27 <th>投稿日</th> 28 <td> <%= t.created_at %></td> 29 </tr> 30 <tr> 31 <th>タグ</th> 32 <td> <%= render 'tweets/tag_list', tag_list: t.tag_list %></td> 33 </tr> 34 35 36 </thead> 37 </table> 38 39 <%= link_to "詳細へ", tweet_path(t.id), class:"link_option" %> 40 <% if t.user_id == current_user.id %> 41 <%= link_to "編集する", edit_tweet_path(t.id), class:"link_option" %> 42 <%= link_to "削除する", tweet_path(t.id), method: :delete, class:"link_option" %> 43 <% end %> 44 </div> 45 </div> 46 <% end %> 47 </div> 48 </div> 49 50</body>
tweets_helper.rb
ruby
1module TweetsHelper 2 include ActsAsTaggableOn::TagsHelper 3end
tweets_controller.rb
ruby
1class TweetsController < ApplicationController 2 before_action :authenticate_user!, except: [:index] 3 #全ての投稿を表示する画面 4 def index 5 # Orderで並び替え 6 @tweets = Tweet.all.order("id DESC") 7 if request.post? 8 if params[:search] == nil 9 @tweets = Tweet.all 10 elsif params[:search] == "" 11 @tweets = Tweet.all 12 else 13 #部分検索 14 @tweets = Tweet.where("address LIKE ? ",'%' + params[:search] + '%').or(Tweet.where("title LIKE ? ", "%" + params[:search] + "%")) 15 end 16 end 17 18 if params[:tag_name] 19 @tweets = @tweets.tagged_with("#{params[:tag_name]}") 20 end 21 end 22 23 def new 24 @tweet = Tweet.new 25 3.times{@tweet.tweet_images.build} 26 end 27 28 #新しいTweetの保存 29 def create 30 @tweet = Tweet.new(tweet_params) 31 #現在ログインしているUserのidをTweetのuser_idという部分にセット 32 @tweet.user_id = current_user.id 33 #新しいTweetの保存に成功した場合 34 if @tweet.save 35 #index.html.erbにページが移る 36 redirect_to action: "index" 37 #新しいTweetsの保存に失敗した場合 38 else 39 #もう一回投稿画面へ 40 redirect_to action: "new" 41 end 42 end 43 #投稿の詳細表示 44 def show 45 @tweet = Tweet.find(params[:id]) 46 @like = Like.new 47 @comments = @tweet.comments 48 @comment = Comment.new 49 @reviews = @tweet.reviews 50 @review = Review.new 51 @tweet_images = @tweet.tweet_images 52 @tweet_image = TweetImage.new 53 if params[:tag_name] 54 @tweets = @tweets.tagged_with("#{params[:tag_name]}") 55 end 56 end 57 #投稿の編集 58 def edit 59 @tweet = Tweet.find(params[:id]) 60 end 61 #投稿の削除 62 def destroy 63 Tweet.find(params[:id]).destroy 64 redirect_to action: :index 65 end 66 67 #アップデート 68 def update 69 @tweet = Tweet.find(params[:id]) 70 if @tweet.update(update_tweet_params) 71 redirect_to :action => "show", :id => @tweet.id 72 else 73 redirect_to :action => "new" 74 end 75 end 76 77 def tag_cloud 78 @tags = Tweet.tag_counts_on(:tags) 79end 80 81 private 82 #セキュリティのため、許可した:bodyというデータだけ取ってくるようにする 83 def tweet_params 84 params.require(:tweet).permit(:body, :name, :money, :weight, :url, :address, :latitude, :longitude, :tag_list, tweet_images_attributes: [:image]) 85 end 86 87 def update_tweet_params 88 params.require(:tweet).permit(:body, :name, :money, :weight, :url, :address, :latitude, :longitude, :tag_list, tweet_images_attributes: [:image, :_destroy, :id]) 89end 90end 91
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/04 05:36
2019/07/04 15:15