Rails 4个嵌套表单数据未保存,有多个通过关联

Rails 4 nested form data not being saved, has many through association

我正在尝试制作一个应用程序,允许人们向一个模型提交信息,同时还从另一个模型中填写一些关于它的数据。目前表单有效,但嵌套表单的数据未保存

这是我的控制器

class Requests::AgesController < ApplicationController
    def new
        @age = Age.new
        @industries = Industry.all.map{ |i| [i.name, i.id ]}
        @age.ages_industries.build
    end

    def create
    @age = Age.new(age_params)

    if @age.save
      flash[:success] = "Age #{@age.name} has been created!"
      redirect_to admin_ages_path
    else
      flash.now[:error] = "Sorry! We were unable to create that age."
      render "new"
    end
  end

  private

  def age_params
    params.require(:age)\
          .permit(:name,
                  :about_title,
                  :about_body,
                  :url,
                  :email,
                  :phone,
                  :street,
                  :city,
                  :state,
                  :zip,
                  :country,
                  :is_published,
                  age_industries_attributes: [:age_id, :industry_id])
  end           
end

年龄模型的相关位

has_many :ages_industries,
    dependent: :delete_all
    accepts_nested_attributes_for :ages_industries


has_many :industries,
    :through => :ages_industries,
    :uniq => true

ages_industries 型号

class AgesIndustry < ActiveRecord::Base
    belongs_to :age, touch: true
    belongs_to :industry
    accepts_nested_attributes_for :industry
    self.primary_key = :id
end

行业模型的相关位

has_many :ages_industries
has_many :ages,
   :through => :ages_industries,
   :uniq => true

形式

section#main
    .wrapper
        h1 Add an Age
        = simple_form_for [:requests, @age] do |form|
            .formElem.m20t.m20b
              = form.error_notification
            .formElem.m20b
              = form.input :name
            .formElem.m20b
              = form.simple_fields_for :ages_industries do |builder|
                    = builder.input :industry_id, collection: @industries, :value => params[:id]
            .fix
            .formElem.m20b
              = form.input :about_title
            .formElem.m20b
              = form.input :about_body
            .formElem.m20b
              = form.input :url
            .formElem.m20b
              = form.input :email
            .formElem.m20b
              = form.input :phone
            .formElem.m20b
              = form.input :street
            .formElem.m20b
              = form.input :city
            .formElem.m20b
              = form.input :state
            .formElem.m20b
              = form.input :zip
            .formElem.m20b.m20t
              = form.input :country, collection: Carmen::country_names
            .fix
            .formElem.m20b
              = form.input :is_published
            .formElem.m20b.m20t
              = form.button :submit, class: "formBtn"
              |  or 
              = link_to "cancel", admin_ages_path

如果有人能指出我正确的方向,将不胜感激。

至少,当您允许嵌套参数时,您缺少复数形式。即年龄而不是年龄。

params.require(:age).permit(ages_industries_attributes: [...])

如果这不能解决问题,我会在您的控制器创建操作中放置一个 prybyebug 并检查 params 和 [=14= 的内容] 以确保它们对齐。