Reputation: 5527
I have a has_many relationship between Trials
and TrialSlots
. This is the app/models/trial.rb
:
class Trial < ActiveRecord::Base
validates :title, :start_date, :trial_duration, :subjects_count, presence: true
has_many :trial_slots
accepts_nested_attributes_for :trial_slots
end
I want to be able to add and edit TrialSlots from the edit form of a Trial, hence I added this to app/admin/trial.rb
form do |f|
# […] some basic inputs skipped for brevity
f.has_many :trial_slots do |trial_slot|
f.inputs "Slots" do
trial_slot.input :start_time, as: :time_picker
trial_slot.input :end_time, as: :time_picker
end
end
f.actions
end
Since I'm using Rails4 I already had to add:
controller do
def permitted_params
params.permit(trial: [ :start_date, :trial_duration, :title,
:break_duration, :subjects_count, :reward, :location,
:agency_name, :agency_address, :contact_name,
:contact_email, :contact_phone, trial_slots_attributes: [:start_date, :end_date] ],
)
end
end
The direct attributes of a Trial are saved as expected. But the attributes of the trial slots are somehow emptied before validation (:start_date and :end_date are both required, thus failing to save). Why?
UPDATE:
I worked around the problem by allowing every parameters.
controller do
def permitted_params
params.permit!
end
end
In this specific case, this is reasonable since it's all inside of /admin
. However I still wonder how to get this working without the workaround.
Upvotes: 0
Views: 901
Reputation: 7749
Your form has start_time
and end_time
, while your params permit start_date
and end_date
. If the code you posted above is correct, that'll likely be your problem.
Upvotes: 1