ruby-on-rails – 在关注wiki之后,仍然在注册编辑中获得“当前密码不能为空”

ruby-on-rails – 在关注wiki之后,仍然在注册编辑中获得“当前密码不能为空”,第1张

概述我担心这里的解决方案很明显,但是我在设计维基上的指令时遇到了麻烦( https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password) 我正在使用Rails 4.在关注维基之后,我仍然收到“当前密码不能为空”.这是我的设置.非常感 我担心这里的解决方案很明显,但是我在设计维基上的指令时遇到了麻烦( https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password)

我正在使用Rails 4.在关注维基之后,我仍然收到“当前密码不能为空”.这是我的设置.非常感谢任何帮助!

REGISTRATIONS_CONTolLER.rb

class RegistrationsController < Devise::RegistrationsController  def update    @user = User.find(current_user.ID)    successfully_updated = if needs_password?(@user,params)      @user.update_with_password(devise_parameter_sanitizer.for(:account_update))      # Rails 3:  @user.update_with_password(params[:user])    else      # remove the virtual current_password attribute update_without_password      # doesn't kNow how to ignore it      params[:user].delete(:current_password)      @user.update_with_password(devise_parameter_sanitizer.for(:account_update))      # Rails 3: @user.update_without_password(params[:user])    end    if successfully_updated      set_flash_message :notice,:updated      # Sign in the user bypassing valIDation in case his password changed      sign_in @user,:bypass => true      redirect_to after_update_path_for(@user)    else      render "edit"    end  end  private  # check if we need password to update user data  # IE if password or email was changed  # extend this as needed  def needs_password?(user,params)    user.email != params[:user][:email] ||      params[:user][:password].present?  endend

APPliCATION_CONTRolER.rb

class ApplicationController < ActionController::Base  protect_from_forgery with: :exception  before_filter :configure_permitted_parameters,if: :devise_controller?  def configure_permitted_parameters    devise_parameter_sanitizer.for(:account_update) do |u|      u.permit(:first_name,:last_name,:username,:email,:avatar,:password,:password_confirmation)    end    devise_parameter_sanitizer.for(:sign_up) do |u|      u.permit(:first_name,:password_confirmation)    end    devise_parameter_sanitizer.for(:sign_in) do |u|      u.permit(:username,:password)    end  endend

的routes.rb

ProjectFoo::Application.routes.draw do  devise_for :users,:controllers => { :registrations => "registrations",:invitations => 'users/invitations' }

注意:我也在使用devise_invitable gem

查看/设计/注册/ EDIT.HTML.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2><%= image_tag @user.avatar.url(:square) %><%= form_for(resource,:as => resource_name,:url => registration_path(resource_name),:HTML => { :method => :put,:class => 'form-horizontal' }) do |f| %>  <%= devise_error_messages! %>  <div >    <div >      <%= f.label :email,:class => 'control-label' %>        <div >          <%= f.email_fIEld :email,:autofocus => true,:class => 'text_fIEld' %>        </div>    </div>    <div >    <%= f.label :username,:class => 'control-label' %>      <div >       <%= f.text_fIEld :username,:class => 'text_fIEld' %>      </div>    </div>    <div >      <%= f.label :first_name,:class => 'control-label' %>        <div >          <%= f.text_fIEld :first_name,:class => 'text_fIEld' %>        </div>    </div>    <div >      <%= f.label :last_name,:class => 'control-label' %>        <div >          <%= f.text_fIEld :last_name,:class => 'text_fIEld' %>        </div>    </div>    <div >      <%= f.label :twitter_handle,:class => 'control-label' %>      <div >        <%= f.text_fIEld :twitter_handle,:class => 'text_fIEld' %>      </div>    </div>    <div >      <%= f.label :avatar,:class => 'control-label' %>        <div >          <%= f.file_fIEld :avatar,:class => 'file_fIEld' %>        </div>    </div>  </div>  <div >    <% if devise_mapPing.confirmable? && resource.pending_reconfirmation? %>      <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>    <% end %>    <div >      <%= f.label :password,:class => 'control-label' %>        <div >          <%= f.password_fIEld :password,:autocomplete => "off",:class => 'password_fIEld' %><br><i>(leave blank if you don't want to change it)</i>        </div>    </div>    <div >      <%= f.label :password_confirmation,:class => 'control-label'  %>        <div >          <%= f.password_fIEld :password_confirmation,:class => 'password_fIEld' %>        </div>    </div>    <div >      <%= f.label :current_password,:class => 'control-label'  %>        <div >         <%= f.password_fIEld :current_password,:class => 'password_fIEld' %><p><i>(we need your current password to confirm your changes)</i></p>        </div>    </div>    <%= f.submit "Update" %>  </div><% end %><div >  <hr></div><div >  <h3>Cancel my account</h3>  <p>Unhappy? <%= button_to "Cancel my account",registration_path(resource_name),:data => { :confirm => "Are you sure?" },:method => :delete %></p>  <p><%= link_to "Back",:back %></p></div>
解决方法 你有两次update_with_password.第二次应该是update_without_password. 总结

以上是内存溢出为你收集整理的ruby-on-rails – 在关注wiki之后,仍然在注册编辑中获得“当前密码不能为空”全部内容,希望文章能够帮你解决ruby-on-rails – 在关注wiki之后,仍然在注册编辑中获得“当前密码不能为空”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存