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

时间:2016-06-10 15:09:55

标签: ruby-on-rails ruby ruby-on-rails-4 nested-forms

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

这是我的控制器

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

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

1 个答案:

答案 0 :(得分:1)

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

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

如果没有解决问题,我会在您的控制器创建操作中添加prybyebug,并查看params和{{1}的内容确保它们对齐。