RoR:在"每次"迭代中显示活动存储映像?



我正试图在推特循环中显示用户配置文件的图片。

我的型号

user.rb
has_many :tweets
tweet.rb
belongs_to :user, optional: true

我的视图

<% @tweets.reverse.each do |tweet| %>
<strong><%= link_to tweet.user.email, thisuser_path(tweet.user_id) %></strong>
<br>
<%= tweets_index_avatar(@image_tweet) %>
....
<% end %>

我的助手

def tweets_index_avatar(image_tweet)
if user.avatar.attached?
image_tag user.avatar.variant(resize: "100x100!"), class: "rounded-circle"
else
image_tag 'default_avatar.jpg', height: 100, width: 100, class: "rounded-circle"
end
end

有了这个(预期(。。。

undefined local variable or method `user'

我试过多种组合

def tweets_index_avatar(image_tweet)
if tweet.user.avatar.attached?
image_tag tweet.user.avatar.variant(resize: "100x100!"), class: "rounded-circle"
else
image_tag 'default_avatar.jpg', height: 100, width: 100, class: "rounded-circle"
end
end

错误

undefined local variable or method `tweet' for 

或者。。。

def tweets_index_avatar(image_tweet)
if tweet.user_id.avatar.attached?
image_tag tweet.user_id.avatar.variant(resize: "100x100!"), class: "rounded-circle"
else
image_tag 'default_avatar.jpg', height: 100, width: 100, class: "rounded-circle"
end
end

相同的结果

我的化身在我的迭代之外工作得很好,但我如何让它们在我的"每次"迭代中工作?ty

您似乎向helper方法传递了不正确的参数(未定义@image_tweet(。我想你想做如下。

我的视图

<% @tweets.reverse.each do |tweet| %>
<strong><%= link_to tweet.user.email, thisuser_path(tweet.user_id) %></strong>
<br>
<%= tweets_index_avatar(tweet) %>
....
<% end %>

我的助手

def tweets_index_avatar(tweet)
if tweet.user.avatar.attached?
image_tag tweet.user.avatar.variant(resize: "100x100!"), class: "rounded-circle"
else
image_tag 'default_avatar.jpg', height: 100, width: 100, class: "rounded-circle"
end
end

最新更新