用户中的参数:: registrationscontroller#创建' 1'不是有效的角色
我正在尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您上传了代码,但是尝试不看到它会更有帮助。.您是否在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' }
在尝试了很多之后,我能够通过更改观点来摆脱此错误。更新的视图看起来像这样
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