我有一个现有的注册控制器和管理面板视图,该管理面板之前是针对iOS和Android制作的应用程序制作的。最近我的任务是使用该面板的sign_up
来注册所有类型的用户,管理员用户和应用程序的用户,因为他们现在想要一个具有相同功能的网页(sign_up
是只在数据库上注册管理员用户,我真的不知道为什么)
管理面板有一个注册新普通用户的功能,看到这一点,我试图在另一个页面中使用相同的方法复制它使用的控制器的方法,使用这种形式<%= form_for @user do |form| %>
。
结果是,如果另一个管理员用户未登录,则不允许注册用户,这就是我移动到设计使用的注册控制器的原始sign_up
页面的原因。
我已经搜索了允许我在sign_up
表单中使用更多参数的方法但到目前为止失败了。这是注册控制器的代码:
class RegistrationsController < Devise::RegistrationsController
before_action :configure_permitted_parameters, if: :devise_controller?
prepend_before_action :require_no_authentication, only: [:new, :create, :cancel]
prepend_before_action :set_minimum_password_length, only: [:new, :edit]
# GET /resource/sign_up
def new
build_resource({})
yield resource if block_given?
respond_with resource
end
# POST /resource
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
# GET /resource/edit
def edit
render :edit
end
# PUT /resource
# We need to use a copy of the resource because we don't want to change
# the current user in place.
def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
resource_updated = update_resource(resource, account_update_params)
yield resource if block_given?
if resource_updated
if is_flashing_format?
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
:update_needs_confirmation : :updated
set_flash_message :notice, flash_key
end
bypass_sign_in resource, scope: resource_name
respond_with resource, location: after_update_path_for(resource)
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
# DELETE /resource
def destroy
resource.destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message! :notice, :destroyed
yield resource if block_given?
respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
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
expire_data_after_sign_in!
redirect_to new_registration_path(resource_name)
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nombre_usuario, :apellidos, :tipo,])
end
def update_needs_confirmation?(resource, previous)
resource.respond_to?(:pending_reconfirmation?) &&
resource.pending_reconfirmation? &&
previous != resource.unconfirmed_email
end
# By default we want to require a password checks on update.
# You can overwrite this method in your own RegistrationsController.
def update_resource(resource, params)
resource.update_with_password(params)
end
# Build a devise resource passing in the session. Useful to move
# temporary session data to the newly created user.
def build_resource(hash=nil)
self.resource = resource_class.new_with_session(hash || {}, session)
end
# Signs in a user on sign up. You can overwrite this method in your own
# RegistrationsController.
def sign_up(resource_name, resource)
sign_in(resource_name, resource)
end
# The path used after sign up. You need to overwrite this method
# in your own RegistrationsController.
def after_sign_up_path_for(resource)
after_sign_in_path_for(resource)
end
# The default url to be used after updating a resource. You need to overwrite
# this method in your own RegistrationsController.
def after_update_path_for(resource)
signed_in_root_path(resource)
end
def sign_up_params
params.require(:usuario).permit(:nombre_usuario, :apellidos, :email, :password, :password_confirmation)
end
def account_update_params
devise_parameter_sanitizer.sanitize(:account_update)
end
def translation_scope
'devise.registrations'
end
end
这是用于sign_up的形式,new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :nombre_usuario %><br />
<%= f.text_field :nombre_usuario, autofocus: true %>
</div>
<div class="field">
<%= f.label :apellidos %><br />
<%= f.text_field :apellidos, autofocus: true %>
</div>
<div class="field">
<%= f.label :tipo %><br />
<%= f.text_field :tipo, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
我只是需要一个解决这个问题的方法而不会杀死移动应用程序使用的东西,我不能在数据库中乱用太多。我也是铁杆新手,所以我真的不知道还有什么问题。当我尝试在 localhost 上运行sign_up
时,它会在NoMethodError
中显示Registrations#new
。当您将:tipo
或:apellido
之类的参数替换为:email
之类的默认身份验证参数时,它可以正常运行并且不会出现错误,因此问题是我尝试验证的额外参数。
是否有一些我没有看到的语法?或者是否为每个用户尝试使用sign_up
而不会杀死混淆其模型的移动应用为时已晚?