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

RoR: Displaying active storage images within an 'each' iteration?

我正在尝试在推文循环中显示用户个人资料照片。

我的模特

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

同样的结果

我的头像在我的迭代之外工作正常,但我如何让他们在我的 'each' 迭代中工作? 泰

您似乎将不正确的参数(@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