简体   繁体   English

将View块转换为helper方法

[英]Convert View block to helper method

I have an Each/do block in my view currently, but I'd prefer to push this code into a helper, as I need to add a few conditional statements in there so I don't want to clutter up my view. 我目前在我的视图中有一个Every / do块,但我更喜欢将这个代码推送到帮助器中,因为我需要在那里添加一些条件语句,所以我不想让我的视图混乱。 Here is the view I have currently that I have been trying to code as a helper method with no luck so far 这是我目前的观点,我一直试图编写一个帮助方法,到目前为止没有运气

<% update.voters_who_voted.each do |voter| %>
  <%= link_to profile_path(voter) do %>
    <%= thirty_avatar(voter) %>
  <% end %>
<% end %>

How would this translate into a helper with this name 如何将此转换为具有此名称的帮助程序

def find_voters_who_voted(update)
  ...
  ...
end

I've tried this with no luck 我试过这个没有运气

def find_voters_who_voted(update)
  update.voters_who_voted.each do |voter|
    link_to profile_path(voter) do
      thirty_avatar(voter)
    end
  end
end

Remember that all you are doing is displaying the return value of this method. 请记住,您所做的只是显示此方法的返回值。 It looks like you are expecting it to act as if it's part of the view, but that's not the way helpers work. 看起来你希望它看起来像是视图的一部分,但这不是帮助者的工作方式。 Something like this would return the equivalent of your original code block 这样的东西会返回原始代码块的等价物

def find_voters_who_voted(update)
  update.voters_who_voted.collect do |voter|
    link_to profile_path(voter) do
      thirty_avatar(voter)
    end
  end.join
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM