没有在嵌套属性上将 Symbol 隐式转换为 Integer

no implicit conversion of Symbol into Integer on nested attributes

我在编辑嵌套属性时遇到问题。我收到此错误:

no implicit conversion of Symbol into Integer

event.rb:

Class Event < ActiveRecord::Base
  has_many :event_joins, :dependent => :destroy
  accepts_nested_attributes_for :event_joins
end

events_controller.rb :

private
   def event_params
     params.require(:event).permit(event_joins_attributes: [:duration]) 
   end

_form.html.erb :

 =f.fields_for :event_joins_attributes do |a|
    =number_field_tag 'event[event_joins_attributes][duration]'
 end

如果我在

允许之前更改我的 params
params[:event][:event_joins_attributes][:duration] = params[:event][:event_joins_attributes][:duration].to_i

我有以下错误:

no implicit conversion of String into Integer

我已经阅读了很多关于批量分配的嵌套属性的帖子,但没有任何效果。 这是我读过的部分帖子。

strong-parameters-permit-all-attributes-for-nested-attributes

rails-4-strong-parameters-nested-objects

whitelisted attributes

当然不想

params.require(:event).permit!

你必须改变这个

=f.fields_for :event_joins_attributes do |a|
   =number_field_tag 'event[event_joins_attributes][duration]'
end

=f.fields_for :event_joins do |a|
   =a.number_field :duration
end

这样您就可以保持 event_params 不变。

小鬼注:

同时始终允许 event_params 中的 :id 以使 Update 正常工作。

def event_params
  params.require(:event).permit(:id, event_joins_attributes: [:id, :duration]) 
end