Rails 部分变量

Rails Partial with variable

用户控制器

def index
    @users = User.all.paginate(page: params[:page])   
end

users/index.html.erb

  <% @users.in_groups_of(3, false).each do |users| %>
    <% users.each do |user| %>
      <% unless current_user == user %>
        <div id="follow">
          ????????????
        </div>
      <% end %> 
    <% end %> 
  <% end %>
  <%= will_paginate @users %>

我想把下面的变成局部的,'users/_follow_form'

<% if current_user.following?(user) %>
  <%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
    html: { method: :delete }, remote: true) do |f| %>
        <%= f.submit "Unfollow", class: "btn btn-primary" %>
    <% end %>
  <% else %>
    <%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
      <div><%= hidden_field_tag :followed_id, user.id %></div>
      <%= f.submit "Follow", class: "btn btn-primary" %>
    <% end %>
  <% end %>
</div>

我需要用什么代码来渲染它?

<div id="follow">
   <%= render partial: "users/follow_form", locals: {passUser: user} %>
</div>

这会将您的 <% users.each do |user| %> 用户 传递给分配为 passUser 的部分用户。您可以在 users/_follow_form 部分中获取此 passUser 变量,例如:

<% if current_user.following?(passUser) %>

检查 this 了解更多。