I've looked at other questions with a similar title and issue, but their solutions do not seem to apply to me. Sorry for not being more descriptive, I was already reaching the 150 limit.
I have the following Models:
class Event < ActiveRecord::Base
has_many :weeks, dependent: :destroy
has_many :tests, through: :weeks
accepts_nested_attribues_for :weeks, allow_destroy: true
accepts_nested_attribues_for :tests, allow_destroy: true
end
class Week < ActiveRecord::Base
belongs_to :event
has_many :tests, dependent: :destroy
end
class Test < ActiveRecord::Base
belongs_to :week
end
And this view:
=form_for @event do |e|
%h3 Event
=render 'form', e: e # This partial contains the general data of the Event.
%h3 Weeks
%table
%thead
%th Start
%th End
%th X
%tbody
=e.fields_for :weeks do |w|
=render 'week_fields', w: w
=link_to_add_fields 'Add Week', e, :weeks # Custom helper that adds the fields of the week to the end of the table via JavaScript.
%h3 Tests
-@weeks.each do |week|
%h5=week.start # Prints the 'start' of the Week.
%table
%thead
%th Start
%th End
%th X
%tbody
= e.fields_for :tests, week.tests do |t|
=render 'test_fields', t: t, s: week.id
=link_to_add_fields 'Add Test', e, :tests, parent: week.id
This works fine for adding Events and Weeks, but when I try to add a new Test I get an error that says:
Cannot modify association 'Event#tests' because the source reflection class 'Test' is associated to 'Week' via :has_many.
I understand this is due to the way I got the associations in the view, but I'm not too sure how I should use them.
This is the code from the link_to_add_fields
helper I'm using above:
def link_to_add_fields(name, f, association, parent: 0)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder, s: parent)
end
link_to(name, '#', class: 'btn btn-default add_fields', id: "add_#{association}", data: {id: id, association: association, fields: fields.gsub("\n", "")})
end
The helper generates code that's later introduced onto the form via JavaScript. It's a modified version of the one used on a Railcasts episode.
This is what I'm using on my controller to limit the parameters:
def event_params
params.require(:event).permit(:name, :start, :end, :place,
tests_attributes: [:id, :name, :date, :time, :week_id],
weeks_attributes: [:id, :start, :end, :_destroy])
end
This works perfectly for editing the data of an already existing Test, but not for new ones generated by the helper. I was reading that (in my case) tests_attributes
should be within week_attributes
, since the Test I'm trying to modify belongs to Week. like this:
def event_params
params.require(:event).permit(:name, :start, :end, :place,
weeks_attributes: [:id, :start, :end, :_destroy, test_attributes: [:id, :name, :date, :time, :week_id]])
end
I do not know how to make the form to follow this association (or if this is actually a solution). I know that Test cannot be modified cause it's being pulled as read-only, but I'm not sure how to arrange the association within the view, so that it can be saved when I update Event.
Any pointers are greatly appreciated.