我試圖包含一個部分並將記錄集合(組織的主持人)傳遞給此部分。在我的組織控制我有:對組織將記錄集合傳遞給部分
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 %>
你在這裏的問題是,部分期望'user'不是'moderator'。本地名稱是從類名稱而不是實例變量名稱中推斷出來的。 [參考](http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html)它表明你可以傳遞一個:as選項,所以可能像'<%= render partial:'users/moderator',collection :@moderators,如::moderator%>'將起作用 – engineersmnky
現在您正在通過相同的ID找到用戶和組織?而你正在調用一個沒有接收者的用戶定義的方法?也許可以嘗試'@ user.remove_modrights(@organization)',但我會確保這部分真的是第一個'@user = User.friendly.find(params [:id]); @organization = Organization.friendly.find(params [:id])''因爲用戶和組織總是共享相同的ID似乎很奇怪。 – engineersmnky
是的,我想我應該從控制器方法中徹底刪除'@user = User.friendly.find(params [:id])'這一行。這是我們訪問的組織頁面,所以這是id中可用的id。那麼,通過該ID搜索@user是沒有意義的... – Nick