ヘルパーで定義したアクションの呼び出したいです。
helpersのusers_helper.rbで定義したimage_for(user)アクションを呼び出したいです。
ruby
1module UsersHelper 2 def image_for(user)←ここのアクション 3 if user.image 4 image_tag "/user_images/#{user.image}", class: "profile_img" 5 else 6 "background: #ccc" 7 end 8 end 9end
home_controller.rbで呼び出したいのですが、
NoMethodError in Home#top
undefined method `image' for nil:NilClass とエラーが出てしまいました。
ruby
1class HomeController < ApplicationController 2 def top 3 if user_signed_in? 4 # 変数@noteにNoteクラスのインスタンスを代入してください 5 @note = Note.new 6 # 変数@notesにNoteクラスのインスタンスの配列を降順で代入してください 7 @notes = Note.all.order(created_at: :desc) 8 render template: "users/show" 9 @users = User.all 10 else 11 @message = "ようこそKotoritterへ!" 12 end 13 end 14 15 def about 16 end 17 18 def timeline 19 @timeline=Timeline.all 20 end 21end 22
とhome_controller.rbに記述したのですが、
ヘルパーで定義したアクションの呼び出し方が分からないです。
showファイル内のusers.html.erを呼び出せば、ヘルパーで定義したものもよびだせると思っていましたが、間違っていますか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。