Home > Blockchain >  ArgumentError in Users::RegistrationsController#create '1' is not a valid role
ArgumentError in Users::RegistrationsController#create '1' is not a valid role

Time:05-18

I am trying to add a drop down menu of User role in a Rails Application. I have configured the devise gem with default parameters. So far I have followed this link to add the user role attribute in User model and the views of Sign up and Log in. roles based authetication using enum

But I am facing an issue. Whenever the sign up operation is performed I get the following error message Error Message

In my Users model I have following code

    class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  enum role: [:manager, :developer, :QA]
end

In my view, I have following code for displaying the drop down menu

<div >
    <%= f.label :role %><br />
    <%= f.select :role, User.roles %>
  </div>

My application controller contains following code

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :email, :password, :role])
    end
end

CodePudding user response:

It would be more helpful if you uploaded your code, but trying without seeing it.. Do you have host variable set in environment.rb? Something such as

config.action_controller.default_url_options = { host: application url' }

CodePudding user response:

After experimenting a lot finally I was able to get rid of this error by changing my views a bit. The updated views looks like this

<div >
    <%= f.label :role %><br />
    <%= f.select :role, ['role#1', 'role#2', 'role#3'] %>
  </div>
  • Related