0

I'm using devise, simple form and role model in my rails 4 app.

I have a user model that devise works with the user model, and i have a separate profile model which contains all the editable functions that a user will interact with.

I have added field to my registration form for users to nominate their role. The roles are defined in the profile model.

My registration form is (the final input field is the relevant one):

<%= 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>

In my profile model (which belongs to the user), I have roles defined as:

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

I also have a field in my user table for the user to select a registration_role. I want users to be able to pick from the list of roles defined in the profile model in the registration form of roles might work (i thought RoleModel allowed that).

Does anyone know how to include a segment in the registration form that has a list of roles (defined in the profile model) so that the selection can then be stored as 'registration_role' in the user table?

Thank you

wintermeyer
  • 8,178
  • 8
  • 39
  • 85
Mel
  • 2,481
  • 26
  • 113
  • 273
  • I think Railscasts has a video of this, e.g.: http://railscasts.com/episodes/188-declarative-authorization Hope that's helpful. – userden Feb 15 '15 at 10:40

1 Answers1

1

You might want to use nested attributes (adding a role_id in your user table)

<%= f.select(:role_id, Role.all.map {|r| [r.zip_code,r.id]})%>

Then simply call user.role to get the information you need

Gryfith
  • 105
  • 4
  • 7