用户中的参数:: registrationscontroller#创建' 1'不是有效的角色

发布于 2025-01-29 12:21:16 字数 1157 浏览 1 评论 0原文

我正在尝试在Rails应用程序中添加用户角色的下拉菜单。我已经配置了使用默认参数设计的宝石。到目前为止,我已经遵循此链接以在用户模型中添加用户角色属性以及注册和登录的视图。

<问题。每当执行注册操作时,我都会收到以下错误消息 错误消息

具有以下代码

    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

在我的用户模型中,我在视图中 ,我有以下标准显示用于显示的代码下拉菜单

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

我的应用程序控制器包含以下代码

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

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 class="field">
    <%= 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2025-02-05 12:21:16

如果您上传了代码,但是尝试不看到它会更有帮助。.您是否在Environment.RB中设置了主机变量?
之类的东西

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

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' }

雨夜星沙 2025-02-05 12:21:16

在尝试了很多之后,我能够通过更改观点来摆脱此错误。更新的视图看起来像这样

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

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 class="field">
    <%= f.label :role %><br />
    <%= f.select :role, ['role#1', 'role#2', 'role#3'] %>
  </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文