Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 在Rails 4中使用has_secure_密码时编辑/更新用户不工作_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 在Rails 4中使用has_secure_密码时编辑/更新用户不工作

Ruby on rails 4 在Rails 4中使用has_secure_密码时编辑/更新用户不工作,ruby-on-rails-4,Ruby On Rails 4,我正在用迈克尔·哈特尔的来制作我自己的用户认证系统。创建新用户时,一切正常。当试图更新一个用户时,事情就不再有意义了。我正在使用has\u secure\u password方法。所以我让它担心获取密码和加密。但是当我更新时,一些奇怪的行为发生了 重要的是,我可以更新用户,而无需输入任何密码及其确认。但如果我输入了密码但没有确认,则需要确认。如果我只输入一个确认密码,则该密码将无误更新。既然has\u secure\u paassword应该做这部分,为什么它在更新时不做呢?由于教程中的这一行,

我正在用迈克尔·哈特尔的来制作我自己的用户认证系统。创建新用户时,一切正常。当试图更新一个用户时,事情就不再有意义了。我正在使用
has\u secure\u password
方法。所以我让它担心获取密码和加密。但是当我更新时,一些奇怪的行为发生了

重要的是,我可以更新用户,而无需输入任何密码及其确认。但如果我输入了密码但没有确认,则需要确认。如果我只输入一个确认密码,则该密码将无误更新。既然
has\u secure\u paassword
应该做这部分,为什么它在更新时不做呢?由于教程中的这一行,我没有在我的用户模型中添加一个状态验证:

密码的存在性验证及其确认如下: 自动添加的具有\u安全\u密码

我知道在这方面也有类似的问题,但似乎没有一个能强制执行强参数,这就是为什么它对我不起作用的原因。下面是我的一些代码

用户控制器

def edit
    @user = User.find(params[:id])
end

def update
    @user = User.find(params[:id])

    # if params[:user][:password].blank?
    #   render 'edit'
    #   return
    # end

    if @user.update(user_params)
        redirect_to @user, notice: 'User successfully updated'
    else
        render 'edit', notice: 'Failed to update profile.'
    end
end
def user_params
    params.require(:user).permit(
        :username, :email, :phone, :bio, :password, :password_confirmation
        )
end
用户模型

class User < ActiveRecord::Base

  before_save {self.email = email.downcase}
  has_secure_password
  before_create :create_remember_token

  validates :username, presence: true, length: {maximum: 255, minimum: 5},
  format: { with: /\A[a-zA-Z0-9]*\z/, message: "may only contain letters and numbers." },
  uniqueness: true

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
  #... Other User stuff.
class用户
编辑表单

<div class="col-xs-8 col-xs-offset-2">
  <%= form_for(@user, :html => {:class => 'well'}) do |f| %>

    <div class="form-group">
      <%= f.label :username %><br />
      <%= f.text_field :username, class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.label :phone %><br />
      <%= f.phone_field :phone, class: 'form-control' %>
    </div>


    <div class="form-group">
      <%= f.label :bio %><br />
      <%= f.text_area :bio, class: 'form-control' %> 
    </div>


    <div class="form-group">
      <%= f.label :email %><br />
      <%= f.email_field :email, class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.label :password %><br />
      <%= f.password_field :password, class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation, class: 'form-control' %>
    </div>

    <div class="form-group">
      <%= f.submit "Update", class: 'btn btn-default' %>
    </div>
  <% end %>

</div>

{:class=>well})do | f |%>







由于这是一个
更新
操作,而不是
创建
操作,因此验证可能未运行

发件人:

验证密码是否存在在创建时,会自动添加密码确认(使用密码确认属性)

安全密码.rb的相关代码部分

def has_secure_password(options = {})
  ...
  if options.fetch(:validations, true)
    validates_confirmation_of :password, if: lambda { |m| m.password.present? }
    validates_presence_of     :password, :on => :create
    validates_presence_of     :password_confirmation, if: lambda { |m| m.password.present? }
  end
  ...
end

能否将用户表单视图添加到问题中?可能是
app/views/user/_form.html.erb
@Andy@franksort我补充说。但我尝试了一些不同的东西,因为我包括报价。似乎
拥有\u安全\u密码
并没有像教程所说的那样。至少在更新方面。创建新用户时工作正常。但在更新时,它似乎无法识别密码存在验证。感谢您的时间@franksort,但如果我将
:password\u digest
添加到强参数中,则无法工作:/Oh哇。我明白了。是的,这确实回答了为什么会这样的问题。非常感谢你的帮助。是否有方法覆盖此操作,以便在
update
操作中也发生此操作?我对rails还是相当陌生:)@Andy很高兴我能帮上忙!类似于
validates:password,presence:true,if:Proc.new{{| user | user.password.present?},confirmation:true,on::update
user
模型中可能有效。