【问题标题】:Rails 4 - Loop through deeply nested params to prevent update of password field to blankRails 4 - 循环通过深度嵌套的参数以防止将密码字段更新为空白
【发布时间】:2015-11-06 04:25:50
【问题描述】:

在编辑表单中,现有密码在表单中显示为空白,这似乎是 Rails 的默认行为。但是,如果未输入新密码,我正在尝试避免密码(或在我的情况下为密码s)更新为空白,类似于此处描述的内容:

Rails Activerecord update saving unedited field as blank

对我来说不同的是,密码字段嵌套更深,而且不止一个。

基本上我有一个小型银行转账应用程序,其中每个:transfer 有两个:transfer_accounts来源目的地(transfer_accounts 是“has_many,通过“转账和账户的连接表),两个转账账户都有一个:account和一个:password属性。

我的尝试在更新操作的顶部是这样的:

params[:transfer][:transfer_accounts_attributes].each do |k, v|
  v[:account_attributes][:password].delete if v[:account_attributes][:password].empty?
end  

这没有用。留空的密码都会更新为空白。

如果留空,我将如何遍历参数并防止其中一个或两个密码更新?

这是我的控制器:

class TransfersController < ApplicationController

  def new
    @transfer = Transfer.new
    @transfer.transfer_accounts.build(account_transfer_role: 'source').build_account
    @transfer.transfer_accounts.build(account_transfer_role: 'destination').build_account
    @valid_banks = Bank.all.collect {|c| [c.name, c.id]}  # available banks seeded in database
  end

  def index
    @transfers = Transfer.all
  end

  def show
    @transfer = resource
  end

  def create
    @transfer = Transfer.new(transfer_params)
    if @transfer.save
      redirect_to transfers_path, notice: "Transfer Created"
    else
      redirect_to transfers_path, alert:  "Transfer Not Created"
    end
  end

  def edit
    resource
    @valid_banks = Bank.all.collect {|c| [c.name, c.id]}  # available banks seeded in database
  end

  def update
    if resource.update_attributes(transfer_params)
      redirect_to transfers_path(resource),     notice: "Transfer Updated"
    else
      redirect_to edit_transfer_path(resource), alert:  "Transfer Not Updated"
    end
  end

  def destroy
    resource.destroy
  end


  private

  def resource
    @transfer ||= Transfer.find(params[:id])
  end

  def transfer_params
    params.require(:transfer).
      permit(:name, :description,
             transfer_accounts_attributes:
               [:id, :account_transfer_role,
                account_attributes:
                  [:id, :bank_id, :name, :description, :user_name,
                   :password, :routing_number, :account_number
                  ]
               ])
  end

end

【问题讨论】:

  • 您能否为请求显示您的params 哈希的内容?
  • 另外,尝试使用blank? 而不是empty? 看看是否有什么不同? v[:account_attributes][:password].blank?
  • 我用的是空白?而不是空的?,但得到了相同的结果。
  • KEY: 0, VALUE: {"account_transfer_role"=>"source", "account_attributes"=>{"bank_id"=>"2", "name"=>"Joe's Checking", " description"=>"Joe 的个人支票账户", "user_name"=>"joechk", "password"=>"change", "account_number"=>"123456789", "routing_number"=>"1122334455", "id" =>"1"}, "id"=>"1"} KEY: 1, VALUE: {"account_transfer_role"=>"destination", "account_attributes"=>{"bank_id"=>"3", "name" =>"Joe's Savings", "description"=>"Joe 的个人储蓄账户", "user_name"=>"joesav", "password"=>"change", "account_number"=>"098765432", "routing_number"= >"0099887766", "id"=>"2"}, "id"=>"2"}

标签: ruby-on-rails-4 controller nested-attributes params


【解决方案1】:
params[:transfer][:transfer_accounts_attributes].each do |k, v|
  v[:account_attributes].delete(:password) if v[:account_attributes][:password].blank?
end

你必须调用hash.delete,而不是删除已经为空的值的内容。 .blank? 也是你的朋友,因为它会处理 nil 和 == ''

【讨论】:

  • 效果很好!我有一种感觉,我正在做你描述的事情,删除一个空白值,但我不确定如何正确处理它。谢谢,詹姆斯! (附言还没有声望投票)
猜你喜欢
  • 2021-12-22
  • 2020-08-09
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
相关资源
最近更新 更多