Snake
Snake

Reputation: 1197

no implicit conversion of Symbol into Integer on nested attributes

I am having issues with editing nested attributes. I'm getting this error :

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

If I change my params before permission with

params[:event][:event_joins_attributes][:duration] = params[:event][:event_joins_attributes][:duration].to_i

I have the following error:

no implicit conversion of String into Integer

I've read a lot of posts about nested attributes for mass-assignment but nothing works. Here is part of posts I've read.

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

rails-4-strong-parameters-nested-objects

whitelisted attributes

Of course, I don't want to do

params.require(:event).permit!

Upvotes: 1

Views: 6002

Answers (1)

Pavan
Pavan

Reputation: 33552

You have to change this

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

to

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

So that you can have your event_params unchanged.

Imp note:

Also always permit :id in the event_params for the Update to work correctly.

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

Upvotes: 7

Related Questions