1

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!
Community
  • 1
  • 1
Snake
  • 1,157
  • 1
  • 10
  • 21

1 Answers1

7

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
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • I get the same error : no implicit conversion of Symbol into Integer after changing the view and adding the ids in the strong paramets – Snake Jul 16 '15 at 09:44
  • @Snake You don't need to do this `params[:event][:event_joins_attributes][:duration] = params[:event][:event_joins_attributes][:duration].to_i` – Pavan Jul 16 '15 at 09:46