如何解决在 f.select 中使用 simpleform 创建必需的选项
我正在尝试以简单形式为 f.select
创建一个默认且禁用的“请选择”选项,并且需要选择一个选项。代码如下:
<div class="form-group mb-3">
<%= f.label :buyer_type,"Which best describes you: " %>
<%= f.select :buyer_type,['Please Select','Option 1','Option 2','Option 3'],{},{ class: "form-control" } %>
</div>
我尝试了多种修复方法,包括 prompt:
、required:
、disabled="disabled">Category</option
、include_blank:
等,但我一直收到语法错误:“ActionView:: User::RegistrationsController#new 中的 SyntaxErrorInTemplate"
所以我查看了 registrations_controller.rb
文件,其中包含以下代码:
# frozen_string_literal: true
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params,only: [:create]
before_action :configure_account_update_params,only: [:update]
# GET /resource/sign_up
def new
params[:role] ||= 'buyer'
build_resource
yield resource if block_given?
render "new_#{params[:role]}"
end
def unconfirmed_registration
flash.delete(:notice)
end
#
# def after_sign_up_path_for(resource)
# # '/an/example/path' # Or :prefix_to_your_route
# new_user_session_path(resource)
# end
# POST /resource
# def create
# super
# end
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
# def update
# super
# end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,# removing all OAuth session data.
# def cancel
# super
# end
protected
# Build a devise resource passing in the session. Useful to move
# temporary session data to the newly created user.
def build_resource(hash = {})
self.resource = resource_class.new_with_session(hash,session)
end
# If you have extra params to permit,append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up,keys: [:buyer_type,:first_name,:last_name,:registered_as])
end
# If you have extra params to permit,append them to the sanitizer.
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update,:quality_inspection,:solicitor,:finance,:business_name,:bio,:abn,:registration,:wsite])
end
# The path used after sign up.
def after_sign_up_path_for(resource)
'/welcome'
end
#
def update_resource(resource,params)
if params[:password].present?
resource.update_with_password(params)
else
resource.update_without_password(params)
end
end
#
def after_update_path_for(resource)
edit_user_registration_path
end
def after_inactive_sign_up_path_for(resource)
unconfirmed_registration_path
end
end
所以我认为它需要在这里或其他地方进行某种验证。我已经尝试在 buyer_type
中包含对 User.rb
的验证,这与我之前与此相关的帖子有关:Creating Required Fields for Website Forms (Simpleform)
我已经考虑使用 f.select
validates :buyer_type
的验证
我不知道哪里出错了!任何帮助将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。