设计 - 无法更新电子邮件或密码 - 使用默认配置

时间:2015-01-29 01:20:19

标签: ruby-on-rails devise

使用我运行生成器时添加的默认代码,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中,我清理了params - 注册所需的那些以及我添加的那些:

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服务器输出。 (我用了#34;我最好的朋友&#34;代表名字,中间名和姓。):

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#编辑为HTML
  参数:{&#34;更新&#34; =&gt; {&#34;名称&#34; =&gt;&#34;用户示例&#34;,&#34; first_name&#34; =&gt;&#34 ;我的&#34;,&#34; middle_name&#34; =&gt;&#34;最佳&#34;,&#34; last_name&#34; =&gt;&#34;朋友&#34;},&#34 ;提交&#34; = GT;&#34;更新&#34;}
  用户负载(0.6ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; = $ 1 ORDER BY&#34;用户&#34;。&#34; id&#34; ASC LIMIT 1 [[&#34; id&#34;,1]]
  在布局/应用程序(6.1ms)内呈现用户/注册/ edit.html.erb
  渲染shared / _bootstrap.html.erb(0.1ms)
  渲染共享/ _html5_shim.html.erb(0.0ms)
  渲染布局/ _navigation_top.html.erb(1.1ms)
  渲染布局/ _messages.html.erb(0.1ms)
  渲染共享/ _navigation_bottom.html.erb(0.5ms)
  渲染shared / _jquery_and_js.html.erb(0.1ms)
在472ms完成200 OK(浏览次数:468.8ms | ActiveRecord:0.6ms)

在rails控制台中查看用户会将第一个,中间名和姓氏的值显示为&#39; nil&#39;。此外,尝试更改电子邮件或密码不起作用。新用户可以注册并保存他们的电子邮件和密码。

我很困惑为什么在安装设计之后这不起作用。也就是说,如何修复它以便用户可以更改他们的电子邮件,密码或添加他们的名字,中间名和姓氏?提前谢谢。

2 个答案:

答案 0 :(得分:0)

不知道这里发生的其他事情(你有很多自定义),但在我看来第一件事是错的:

您允许:用户作为params的根目录:devise_parameter_sanitizer.for(:users) { |u| u.params.permit(:id, :email, :password, :password_confirmation)} 但是使用:update(???)作为param hash的根来创建一个表单:
<%= form_for(@user, as: :update, url: users_path(params[:id]), html: { method: :put, :multipart => true }) do |f| %>


首先将表单更改为<%= form_for(@user, as: :user, url: users_path(params[:id]), html: { method: :put, :multipart => true }) do |f| %>

答案 1 :(得分:0)

我无意中 - 前一段时间 - 删除了_form.html.erb部分设置在views / users文件夹中的部分。然后我还删除了从users/edit.html.erb文件到部分的链接。为了弄清楚这一点,我不得不建立一个新项目。这不是路由错误。

相关问题