表单下拉选项未保存在数据库中

时间:2017-04-04 17:14:55

标签: ruby-on-rails ruby-on-rails-4

我正在使用与Devise属于注册和帐户更新页面的表单。我已经为用户模型添加了2个字段,角色和性别。

我将我的角色字段设置为:

<%= f.select :role, options_for_select([['Student - High School'], ['Student - Undergraduate'], ['Student - Nursing'], ['Student - Graduate'], ['Student - Medical'], ['Health Care - Resident'], ['Health Care - Nurse'], ['Health Care - Physician'], ['Health Care - Staff'], ['Other']]), class: 'form-control'  %>

和性别:

<%= f.select :gender, options_for_select([['Male'], ['Female'], ['Other']]), class: 'form-control' %>

在我的应用程序控制器中,我已经让Devise允许:

def configure_permitted_parameters
 devise_parameter_sanitizer.permit(:sign_up, keys: [ :email, :password, :password_confirmation, :age, :name, role: [], gender:[]])
 devise_parameter_sanitizer.permit(:account_update, keys: [ :email, :password, :password_confirmation, :age, :name, role: [], gender: []])
end
你能帮我找到我犯的错误吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

您应该将您的设计控制器放入您的应用程序。

rails generate devise:controllers [scope]

然后将允许的params方法放在注册控制器

class Users::RegistrationsController < Devise::RegistrationsController

  before_action : configure_permitted_parameters
  private
  #Your methods here
end

路线: devise_for :users, :controllers => {:registrations => "registrations"}

答案 1 :(得分:0)

在视图中尝试此操作:

options_for_select(['Student - High School', 'Student - Undergraduate', 'Student - Nursing', 'Student - Graduate', 'Student - Medical', 'Health Care - Resident', 'Health Care - Nurse', 'Health Care - Physician', 'Health Care - Staff', 'Other'], selected: 'Student - High School')

options_for_select(['Male', 'Female', 'Other'], selected: 'Male')

这在控制器中:

devise_parameter_sanitizer.permit(:sign_up, keys: [ :email, :password, :password_confirmation, :age, :name, :role, :gender ])

devise_parameter_sanitizer.permit(:account_update, keys: [ :email, :password, :password_confirmation, :age, :name, :role, :gender ])

相关问题