【问题标题】:no implicit conversion of Symbol into Integer on nested attributes嵌套属性上没有将 Symbol 隐式转换为 Integer
【发布时间】:2015-07-16 09:28:41
【问题描述】:

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

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!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-attributes strong-parameters


    【解决方案1】:

    你必须改变这个

    =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 以使更新 正常工作。

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

    【讨论】:

    • 我得到同样的错误:更改视图并在强参数中添加 id 后没有将 Symbol 隐式转换为 Integer
    • @Snake 你不需要这样做params[:event][:event_joins_attributes][:duration] = params[:event][:event_joins_attributes][:duration].to_i
    猜你喜欢
    • 2014-03-12
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多