首页 > 解决方案 > Rails 合并(和手动分配)分配 nil

问题描述

我正在尝试使用 Rails 5.1 中的强参数在控制器中创建一个模型(对于 strong_params,有些东西与以前的有所不同)。但是,当我检查参数时,合并的参数不存在,并且我得到一个 ForbiddenAttributesError 跟踪到下面的 Model.new 行。模型中唯一的事情是验证所有属性的存在。

class ModelController < ApplicationController
  before_action :application_controller_action

  def create
    @model = Model.new(strong_params)
    if @model.valid?
      result = @model.save
    else
      render html: 'MODEL NOT VALID'
    end
    render html: 'DONE'
  end

  private
    def strong_params
      # attr_1 and attr_2 are set in the application controller and are available here.
      params.require(:model).permit(:name, :attribute_1, :attribute_2).merge(attribute_1: @attr_1, attribute_2: @attr_2)
      # Inserting the following two lines causes a ForbiddenAttributesError
      puts params.inspect  # DOES NOT INCLUDE @attr_1 and/or @attr_2
      return params
    end

我可能做错了什么,因为我什至尝试将强参数放入具有属性的模型中(我之前可以检查),但它仍然失败,因为模型中 attr_1 和 attr_2 的验证失败。

def create
  puts @user.inspect  (not nil)
  @model = Model.new(name: strong_params[:name], attribute_1: @attr_1, attribute_2: @attr_2)

更新:

好的,我在故障排除中遇到了一些奇怪的错误。似乎合并无法正常工作,尽管我确信它是在某一时刻。

我检查的第一件事是@attr_1 和@attr_2,它们肯定已经设置好了。

出于故障排除的目的,我将应用程序 before_action 简化为:

def application_before_action
  @attr_1 = Model.first
  @attr_2 = Model.last

使用上面的代码,检查 params 对象,然后在 require().permit() 之后返回它,我得到一个 ForbiddenAttributesError(没有说明是什么)。如果我删除这些行,我会从模型中得到一个缺少属性的错误,表明 @attr_1 和 @attr_2 丢失。

更新 2

更改了问题的标题,因为我可能在故障排除过程中感到困惑。我认为问题只是合并分配为零......但奇怪的是(我自己最初)建议的手动分配和此处的另一个答案。属性键在那里,但它们被分配为零。另外,请注意我的示例使用的是单个模型,而实际上有两个模型,模型 1 和模型 2。我正在将 Model1 中的值分配给 Model2。

这是错误的更好演示:

def create
    puts '0:'
    puts @model1.inspect
    puts '1:'
    puts strong_params.inspect
    @model2 = Model2.new(strong_params) do |m|
      m.user_id = @attr_1
      m.account_number = @attr_2
    end
    puts '3:'
    puts @model2.inspect

    if @model2.valid?
      result = @model2.save
      render html: 'SUCCESS' and return
    else
      render html: @model2.errors.full_messages and return
    end
end

控制台中的输出:

0:
#<Model1 id: 29, attribute_1: 'test_value_1', attribute_2: 'test_value_2', created_at: "2018-08-15 03:55:08", updated_at: "2018-08-15 04:05:01">
1:
<ActionController::Parameters {"name"=>"test_name", "attribute_1"=>nil, "attribute_2"=>nil} permitted: true>
3:
#<Model2 id: nil, name: 'test_name', attribute_1: nil, attribute_2: nil, created_at: nil, updated_at: nil>

显然 nil id 和 timestamps 是因为模型还没有被保存。

html model2.errors.full_messages 是: ["attribute_1 can't be blank", "attribute_2 can't be blank"]

解决方案

之前来自纯 ruby​​ 环境,我误认为 ActiveRecord 模型的默认访问器。删除访问器似乎已经解决了这个问题。

标签: ruby-on-railsstrong-parametersruby-on-rails-5.1rails-models

解决方案


您可以一一分配奇数值,而不是使用 params 散列:

class ModelController < ApplicationController
  before_action :application_controller_action

  def create
    @model = Model.new(strong_params) do |m|
      m.attribute_1 = @attr_1
      m.attribute_2 = @attr_2
    end

    if @model.valid?
      result = @model.save
    else
      render html: 'MODEL NOT VALID'
    end
    # don't do this it will just give a double render error
    render html: 'DONE'
  end

  private

   private
    def strong_params
      params.require(:model).permit(:name, :attribute_1, :attribute_2)
    end
end

一般来说,这是一种更易读的方式,例如将参数与会话中的值合并。

您的强参数方法不起作用的原因是它以各种可能的方式被破坏了。要点是您没有返回列入白名单和合并的参数哈希。你要归还整个shebang。

您似乎也有错误的印象.require.permit并且.merge更改了原始哈希 - 他们没有 - 他们返回一个新的哈希(实际上是一个 ActionContoller::Parameters 实例)。

def strong_params
  # attr_1 and attr_2 are set in the application controller and are available here.
  permitted = params.require(:model).permit(:name, :attribute_1, :attribute_2)
        .merge(attribute_1: @attr_1, attribute_2: @attr_2)
  puts permitted.inspect
  permitted # return is implicit
end

要不就:

def strong_params
  # attr_1 and attr_2 are set in the application controller and are available here.
  params.require(:model).permit(:name, :attribute_1, :attribute_2)
        .merge(attribute_1: @attr_1, attribute_2: @attr_2)
end

推荐阅读