无法使用 field_for 和 rails 在参数中定义散列 5

Cant define a hash in the params, using field_for and rails 5

我正在尝试向 rails 范围提交 2 个值。我需要半径(由范围标签提交)和邮政编码。

我在表单中有一个 fields_for 标签(在 haml 中):

      =f.fields_for :postcode do |postcode_fields|
        =postcode_fields.text_field(:postcode_d,placeholder: "HQ Location")
        =postcode_fields.range_field(:max_range, in: 1..100)

范围:

scope :postcode, lambda {|input|
return nil  if input.blank?

terms = Scotland.near(input[0], input[1])

where(
terms.map { |term|
  " charity_id = ?"
}.join(' OR '),
*terms.map {|e|(e.charity_id)}.flatten
)
}

控制器:

    def scotland_params
      params.require(:scotland).permit!(:charity_number, :charity_name, :registered_date, :known_as, :charity_status, postcode: [:postcode_d,:max_range] , :constitutional_form)
    end

问题似乎出在我对邮政编码的定义上。我尝试了多种方法来定义邮政编码哈希,其中 none 有效。对于我在这里使用的错误是

SyntaxError 

    (/Users/peterkentish/Documents/UNIVERSITY/SecondYear/Semester2/SoftwareHut/project/app/controllers/scotlands_controller.rb:46: syntax error, unexpected =>
..., :charity_status, postcode: =>[:postcode_d, :max_range] , :...
...                               ^
/Users/peterkentish/Documents/UNIVERSITY/SecondYear/Semester2/SoftwareHut/project/app/controllers/scotlands_controller.rb:46: syntax error, unexpected ',', expecting keyword_end
...: =>[:postcode_d, :max_range] , :constitutional_form, :previ...
...                               ^):

长话短说;博士 如何在 rails 5 中传递嵌套属性?

编辑:

参数哈希(我认为):

@scotlands = Scotland.all
@filterrific = initialize_filterrific(Scotland, params[:filterrific]) or return
 @scotlands = @filterrific.find.page(params[:scotlands])
@scotlands = @scotlands.paginate(page: params[:page], per_page: 20)

respond_to do |format|
format.html
format.js

真正的问题是为什么每个人在编写强大的参数并将整个定义塞进一行时变得完全疯狂。

不可读的代码是您出错的原因。

def scotland_params
  params.require(:scotland).permit(
    :charity_number, :charity_name, :registered_date, 
    :known_as, :charity_status, :constitutional_form,
    postcode_attributes: [:postcode_d, :max_range]
  )
end

我相信您希望将 :postcode => [:postcode_d, :max_range] 换行到 {...} 中,或者将 :postcode => [:postcode_d, :max_range] 移动到 permit! 参数列表的末尾。

SyntaxError 是 Ruby,在解释您的代码时,由于没有明确的 {...}.

,因此在 :postcode 散列之后不期望任何内容

如果有人遇到这个问题,那是 gem 过滤器中存在错误:

https://github.com/jhund/filterrific/pull/116

感谢帮助过的人