0

我有以下表格:

projects
users
projects_users <- relational table

在视图中,我循环浏览所有项目,并根据用户与它的关系(所有者、成员、而不是成员)将它们呈现为具有不同类(以及链接或未链接)的一个和一个。

这很好用,除了第 3 行的 if 语句,我在其中检查用户是否是成员。'check_if_member' 可以在项目助手中找到(见下文)。发生的情况是我收到以下错误消息:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

...这(如果我没记错的话)意味着“@project.id”或“current_user.id”都是零。在这种情况下,我很确定它是 nil 的 @project.id,并且它实际上没有传递给助手。

如果我是对的,我该如何将它传递给助手?如果我错了,那有什么问题?

风景:

1.  <% @projects.each do |project| %>
2.      <div class="project_container">
3.          <% if check_if_member %>
4.              <% if project.user_id == current_user.id %>
5.                  <div class="users_project label label-success">
6.                      Owner
7.                  </div>
8.              <% else %>
9.                  <div class="not_users_project label label-info">
10.                     Member
11.                 </div>
12.             <% end %>
13.             <div class="project_name_div">
14.                 <%= link_to (project.title), project_path(project) %><br />
15.             </div>
16.         <% else %>
17.             <div class="project_name_div">
18.                 project.title <br />
19.             </div>
20.         <% end %>
21.     </div>
22. <% end %>

帮手:

module ProjectsHelper
    def check_if_member
        if ProjectsUser.where("project_id = ? AND user_id = ?", @project.id, current_user.id)
            return true
        else
            return false
        end
    end
end
4

1 回答 1

2

显式传递

def check_if_member project, user
    ProjectsUser.where("project_id = ? AND user_id = ?", project.id, user.id).count > 0
end

然后

<% if check_if_member(project, current_user) %>
于 2013-02-12T00:30:39.847 回答