【发布时间】:2015-12-29 03:11:31
【问题描述】:
我正在尝试使用 Rails 4 制作应用程序。我使用引导程序、设计简单的表单和角色模型 gem。
profile.rb
class Profile < ActiveRecord::Base
include RoleModel
#reminder for roles (RoleModel): always add new roles to the end of this line - don't change the order
roles :admin, :manager,
:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager,
:sponsor,
:project_manager, :representative, # for both uni and industry
:grantor, :investor, :adviser, :innovation_consultant, :panel_member,
:participant, :guest, :pending
# Other stuff goes here
end
然后在我的个人资料视图中,我有一个表单,我可以在其中询问用户来自哪里。我想根据用户的角色以不同的方式表达问题,并且我想根据该角色使用不同的选项数组。例如,如果当前用户是学生,则该数组应填充大学列表。
<div class="col-md-3 col-md-offset-1">
<% if current_user.profile.has_any_role?(:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager) %>
<%= f.label 'Select your university', :class => 'question-project' %>
<% elsif current_user.profile.has_role?(:sponsor) %>
<%= f.label 'Select your organisation', :class => 'question-project' %>
<% elsif current_user.profile.has_any_role?(:grantor, :investor, :adviser, :innovation_consultant, :panel_member) %>
<% end %>
</div>
<div class="col-md-7">
<div class="response-project">
<% if current_user.profile.has_any_role?(:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager) %>
<%= f.collection_select :university_id, University.all, :id, :name, prompt: 'Select your university' %>
<% elsif current_user.profile.has_role?(:sponsor) %>
<%= f.collection_select :organisation_id, Organisation.all, :id, :name, prompt: 'Select your organisation' %>
<% elsif current_user.profile.has_any_role?(:grantor, :investor, :adviser, :innovation_consultant, :panel_member) %>
<%= f.collection_select :firm_id, Firm.all, :id, :name, prompt: 'Select your organisation' %>
<% else %>
<%= current_user.profile.id %>
<% end %>
我有最后的 else 语句来证明以上所有方法都不起作用。当我尝试这个时,使用具有角色的用户:student,应该让我沿着第一条路线前进。它不起作用。我得到的是 user.profile.id。
谁能看到我做错了什么?
【问题讨论】:
-
<%= puts current_user.profile.has_any_role?(:student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager) %>什么是布尔值,请把结果粘贴到这里。 -
<%= puts current_user.profile.has_role? :student %>,这个o/p是什么? -
什么也没给。如果我尝试不使用“puts”,我会得到错误的。如果我尝试在控制台中使用个人资料 ID,我会得到 role = student