在 rails 中更新参数散列

update params hash in rails

我需要一种修改参数散列的方法。我尝试了几件事,但 none 确实有效。

Unpermitted parameters: :players, :leaders

参数

{"utf8"=>"✓", "authenticity_token"=>"asa", "user"=>{"first_name"=>"anikeeet", "last_name"=>"tiwariii", "friend_attributes"=>{"players"=>"50589,50590", "leaders"=>"16,6,18", "phone_number"=>"", "title"=>"aasdassd", "role"=>"abcdef"}}}

我这样试过

(friend_params.dig(:friend_attributes) || []).each do |_, attributes|
      attributes['players'] = attributes['players'].split(',').map(&:to_i)
      attributes['leaders'] = attributes['leaders'].split(',').map(&:to_i)
    end

这样

(friend_params.dig(:friend_attributes) || []).each do |key, val|
  friend_params["friend_attributes"]["#{key}"] = val.split(',').map(&:to_i) if key == 'players' || key == 'leaders'
end

这样

(params.dig(:user, :friend_attributes) || []).each do |key, val|
  params["user"]["friend_attributes"]["#{key}"] = val.split(',').map(&:to_i) if key == 'players' || key == 'leaders'
end

然后这样

 if (attributes = params.dig(:user, :friend_attributes))
      %i[players leaders].each do |key|
        next unless attributes.has_key?(key)
        attributes[key] = attributes[key].split(',').map(&:to_i)
      end
    end

但是 none 的方法对我有用。我正在分享我的完整控制器代码

class FriendsController < ApplicationController

  def update
    # (friend_params.dig(:friend_attributes) || []).each do |_, attributes|
    #   attributes['players'] = attributes['players'].split(',').map(&:to_i)
    #   attributes['leaders'] = attributes['leaders'].split(',').map(&:to_i)
    # end

    # (friend_params.dig(:friend_attributes) || []).each do |key, val|
    #   friend_params["friend_attributes"]["#{key}"] = val.split(',').map(&:to_i) if key == 'players' || key == 'leaders'
    # end

    # (params.dig(:user, :friend_attributes) || []).each do |key, val|
    #   params["user"]["friend_attributes"]["#{key}"] = val.split(',').map(&:to_i) if key == 'players' || key == 'leaders'
    # end

     if (attributes = params.dig(:user, :friend_attributes))
       %i[players leaders].each do |key|
         next unless attributes.has_key?(key)
         attributes[key] = attributes[key].split(',').map(&:to_i)
       end
     end


    if current_user.update(friend_params)
      render json: { success: true }
    else
      render json: {error: "something went wrong"}
    end
  end

  private
  def friend_params
    params.require(:user).permit(:first_name, :last_name, friend_attributes: [:phone_number, :title, :role, :players, :leaders])
  end
end

协会

Class User < ApplicationRecord 
  has_one :friend, foreign_key: :user_id, dependent: :destroy
  accepts_nested_attributes_for :friend
end

class Friend < ApplicationRecord
  self.table_name = 'schema.friends'
  belongs_to :user
end

players和leader是friends中存储的数组字段table 谁能告诉我做错了什么?

你的参数是哈希,require 和 permit 方法是 ActionController::Parameters 的实例方法。您也可以尝试按符号而不是字符串进行挖掘。

params.dig('user', 'friend_attributes')

或者如果你想继续使用这个符号你可以这样写

params.with_indifferent_access.dig(:user, :friend_attributes)

请详细阅读 with_indifferent_access 方法。

:)

像这样的东西就可以完成工作:

if (attributes = params.dig(:user, :friend_attributes))
  %i[players leaders].each do |key|
    next unless attributes.has_key?(key)
    attributes[key] = attributes[key].split(',').map(&:to_i)
  end
end

目前的问题在于 (friend_params.dig(:friend_attributes) || []).each 循环遍历键值对,或者如果不存在则通过空数组循环。然后,您对该值(它是一个字符串)调用 #[] 方法。例如。 "50589,50590"['players'] 尝试在 "50589,50590" 中找到子字符串 'players',结果应该是 nil。随后出现 "NoMethodError: undefined method `split' for nil:NilClass" 异常。

您应该简单地使用返回的集合,而不是遍历键值对。


更好的问题是,为什么你需要转换参数?

视图不能这样排列来提供数组格式的参数吗?

<input name="user[friend_attributes][players][]" type="checkbox" value="1" />
<input name="user[friend_attributes][players][]" type="checkbox" value="2" />
<input name="user[friend_attributes][players][]" type="checkbox" value="3" />

首先将数字数组发送到控制器,无需将参数转换为正确的格式。

注意:如果您以上述格式提供参数,您应该将允许的参数更新为:

params.require(:user).permit(:first_name, :last_name, friend_attributes: [:phone_number, :title, :role, players: [], leaders: []])
# players and leaders are now expecting an array instead of a scalar value