设计,角色和简单形式

时间:2015-02-15 08:49:02

标签: ruby-on-rails devise simple-form user-roles

我在rails 4 app中使用了设计,简单的形式和角色模型。

我有一个用户模型设计用户模型,我有一个单独的配置文件模型,其中包含用户将与之交互的所有可编辑功能。

我在我的注册表单中添加了字段,供用户提名他们的角色。角色在概要模型中定义。

我的注册表格是(最终输入字段是相关的):

<%= simple_form_for(resource, as: resource_name, :html => {:id => "sign_up_user"}, url: registration_path(resource_name)) do |f| %>

<%= f.error_notification %>

<div class="form-inputs" style="padding-left:20%; text-align:left; color:black;">
  <%= f.input :first_name, placeholder: 'Enter your first name', autofocus: true, required: true, :input_html => {:maxlength => 15, :size => 40} %>
  <%= f.input :last_name, placeholder: 'Enter your surname', autofocus: true, required: true, :input_html => {:maxlength => 15, :size => 40} %>
  <%= f.input :email, placeholder: 'Enter email',  autofocus: true, required: true, :input_html => {:maxlength => 35, :size => 40} %>
  <%= f.input :password, placeholder: 'At least 8 characters', required: true, :input_html => {:maxlength => 15, :size => 40} %>
  <%= f.input :password_confirmation, placeholder: 'Confirm your password',  required: true, :input_html => {:maxlength => 15, :size => 40} %>
  <%= f.input :role do %>
  <%= f.select :role, Role.all.map { |r| [r.name, r.id] }, include_blank: true %>
  <% end %>
</div>

在我的个人资料模型(属于用户)中,我将角色定义为:

roles :admin, :manager, :student, :educator, :guest

我的用户表中还有一个字段供用户选择registration_role。我希望用户能够从角色注册表单中的配置文件模型中定义的角色列表中选择可能有效(我认为RoleModel允许这样做)。

有没有人知道如何在注册表单中包含一个具有角色列表(在配置文件模型中定义)的段,以便选择可以在用户表中存储为“registration_role”?

谢谢

1 个答案:

答案 0 :(得分:1)

您可能希望使用嵌套属性(在用户表中添加role_id)

shinyApp()

然后只需调用user.role即可获取所需信息