Вложенные атрибуты «многие ко многим» в Rails 4 (с сильными параметрами)
Я пытался выяснить это в течение нескольких дней. Я использую Rails 4 (с обновленной техникой массового назначения) и пытаюсь использовать вложенные атрибуты с отношением «многие ко многим». Моя запись сохраняется в БД, но все равно нулю, и я 'я получаюНедопустимые параметры: школа, выпускники, перспективы » ошибка в моих логах.
Вот'Что у меня есть:
referral.rb
class Referral < ActiveRecord::Base
belongs_to :school
belongs_to :alumni
belongs_to :prospect
end
alumni.rb
class Alumni < ActiveRecord::Base
has_many :referrals
has_many :prospects, through: :referrals
accepts_nested_attributes_for :referrals
end
school.rb
class School < ActiveRecord::Base
has_many :referrals
has_many :prospects, through: :referrals
has_many :alumnis, through: :referrals
accepts_nested_attributes_for :referrals
end
prospect.rb
class Prospect < ActiveRecord::Base
has_many :referrals
has_many :alumnis, through: :referrals
accepts_nested_attributes_for :referrals
end
referrals_controller.rb
def create
@referral = Referral.create(referral_params)
respond_to do |format|
if @referral.save
# ReferralMailer.referrer_email(@referral).deliver
# ReferralMailer.referral_email(@referral).deliver
format.html { redirect_to @referral, notice: 'Referral was successfully created.' }
format.json { render action: 'show', status: :created, location: @referral }
else
format.html { render action: 'new' }
format.json { render json: @referral.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_referral
@referral = Referral.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def referral_params
params.require(:referral).permit(prospects_attributes: [:first_name, :last_name, :email], alumnis_attributes: [:first_name, :last_name, :email], schools_attributes: [:name])
end
_form.html.erb
prohibited this referral from being saved:
вывод журнала сервера
Processing by ReferralsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ee+rREUU/0wGzNFTEaMxr8oRStaA53X9fmDrlVRyrD8=", "referral"=>{"school"=>{"name"=>"asdf"}, "alumnis"=>{"first_name"=>"asdf", "last_name"=>"asfd", "email"=>"asdf"}, "prospects"=>{"first_name"=>"asdf", "last_name"=>"asdf", "email"=>"asdf"}}, "commit"=>"Create Referral"}
Unpermitted parameters: school, alumnis, prospects
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "referrals" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Fri, 12 Jul 2013 03:49:06 UTC +00:00], ["updated_at", Fri, 12 Jul 2013 03:49:06 UTC +00:00]]
(0.6ms) commit transaction
(0.1ms) begin transaction
(0.1ms) commit transaction
Redirected to http://localhost:3000/referrals/68
Реферальная запись
=> #