Home > other >  How to set the user creation through devise gem to be by default developer
How to set the user creation through devise gem to be by default developer

Time:08-19

I am working on a rails application (ruby 3 and rails 7). I have two models. One is User(which comes by devise gem) and the other one is Role. User belongs to role and role has many users. What I am trying to do is when a new user is signing up, he/she should be by default developer.

The one way through which I solved this is by writing default value in the migration file of users but that approach is prohibited by my supervisor. Is there any other way. I dont want to use rolify or any other gems for roles and I have separate table for roles.

Roles: Admin, Developer

CodePudding user response:

I did it. I create some fields when name, bio... When using gem devise

Gem devise https://www.rubydoc.info/github/plataformatec/devise/Devise/ParameterSanitizer

In devise/registrations/new.html.erb


<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>
 // Hidden role_id. EX: role_id is 1 as Developer
    <%= f.hidden_field :role_id, autofocus: true, value: "1", autocomplete: "name" %>

  <div >
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div >
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "new-password" %>
  </div>

  <div >
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>

  <div >
    <%= f.submit "Sign up" %>
  </div>
<% end %>

class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:role_id])
    devise_parameter_sanitizer.permit(:account_update, keys: [:website, :bio])
  end

end
  • Related