【发布时间】:2015-03-28 03:12:31
【问题描述】:
Devise 不会使用我运行生成器时添加的默认代码保存新的电子邮件或密码。我发现在添加了名字、中间名和姓氏字段然后重新开始之后。默认代码为:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
我在registrations/edit 表单中添加了一些字段,特别是:first_name、:middle_name 和:last_name
根据 Stackoverflow 上的其他示例,我更改了 views/users/registrations/edit.html.erb 文件中的资源名称和路径。:
<%= form_for(@user, as: :update, url: users_path(params[:id]), html: { method: :put, :multipart => true }) do |f| %>
Devise 位于我的 config/routes 文件的顶部,范围为:
devise_for :users
devise_scope :user do
put "update", to: 'devise/registrations#update', as: :update
end
在 ApplicationController 中,我清理了参数 - 注册所需的参数和我添加的参数:
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:users) { |u| u.params.permit(:id,
:email, :password, :password_confirmation)}
devise_parameter_sanitizer.for(:users) { |u| u.permit(
:first_name, :middle_name, :last_name) }
end
end
我还把惯用的 before_filter 放在了 ApplicationController 的顶部:
before_action :configure_permitted_parameters, if: :devise_controller?
更新方法如下:
def update
@user = User.find(params[:id])
if params[:user][:password].blank?
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
if @user.update_attributes(users_params)
sign_in(current_user, :bypass => true) if @user == current_user
@user.save
flash[:success] = "Profile Updated!"
redirect_to user_path
else
redirect_to edit_user_path
end
end
这是 rails 服务器的输出。 (我用“我最好的朋友”作为名字、中间名和姓氏。):
Started GET "/users/edit?update%5Bname%5D=User+Example&update%5Bfirst_name%5D=my&update%5Bmiddle_name%5D=best&update%5Blast_name%5D=friend&commit=Update" for ::1 at 2015-01-28 18:54:57 -0600<br/>
由 Devise::RegistrationsController#edit 处理为 HTML
参数:{"update"=>{"name"=>"User Example", "first_name"=>"my", "middle_name"=>"best", "last_name"=>"friend"}, "commit" =>“更新”}
用户负载 (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
在布局/应用程序中呈现用户/注册/edit.html.erb (6.1ms)
渲染 shared/_bootstrap.html.erb (0.1ms)
渲染 shared/_html5_shim.html.erb (0.0ms)
渲染布局/_navigation_top.html.erb (1.1ms)
渲染布局/_messages.html.erb (0.1ms)
渲染 shared/_navigation_bottom.html.erb (0.5ms)
渲染 shared/_jquery_and_js.html.erb (0.1ms)
在 472 毫秒内完成 200 次 OK(查看次数:468.8 毫秒 | ActiveRecord:0.6 毫秒)
在 Rails 控制台中查看用户时,名字、中间名和姓氏的值显示为“nil”。此外,尝试更改电子邮件或密码也不起作用。新用户可以注册并保存他们的电子邮件和密码。
我很困惑为什么这在安装设计后不起作用。也就是说,我该如何解决它,以便用户可以更改他们的电子邮件、密码或添加他们的名字、中间名和姓氏?提前谢谢你。
【问题讨论】:
标签: ruby-on-rails devise