将记录集合传递给部分记录

时间:2015-06-29 18:55:02

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller

我试图包含一个部分并将一组记录(一个组织的版主)传递给这个部分。在我的组织控制器中,我有:

def show
  @organization = Organization.friendly.find(params[:id])
  @users = @organization.users
  @moderators = User.with_role(:moderator, @organization)
end

显示组织的视图(第一部分工作,但第二部分不工作):

<%= render @users %>
<%= render 'users/moderator', collection: @moderators %>

结果是一条错误消息:undefined local variable or method 'moderator' for #<#<Class:0x00000004957488>:0x007f5f51891220>引用部分中的<% if moderator.activated? %>

在主持人部分我想只显示激活的主持人:

<% if moderator.activated? %>
  <td><%= link_to moderator.username, user_path(moderator) %></td>
  <td><%= moderator.fullname %></td>
  <td>
    <%= link_to image_tag("delete.gif", title: "remove as moderator", class: 'profile-icon-small'), 
                data: { user_id: moderator.id, organization_id: @organization.id, method: :post, url: removemoderator_path, confirm: "Are you certain?" } %>
  </td>
<% end %>

1 个答案:

答案 0 :(得分:0)

试试这个

<%= render partial: 'users/moderator', collection: @moderators %>

<强>更新

<%= link_to image_tag("delete.gif", title: "remove as moderator", class: 'profile-icon-small'), 
            data: { user_id: user.id, organization_id: @organization.id, method: :delete, url: removemoderator_path, confirm: "Are you certain?" } %>

的routes.rb

delete 'removemoderator/:id'=>  'users#remove_moderator', as: 'removemoderator'