0

我已经这样做了 10 次,每次我似乎都遇到了嵌套表单的一些问题。这是我所拥有的:

客户端控制器:

  def new
    @client = Client.new
    @contact = @client.contacts.new
    @header = "New Client"

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @client }
    end
  end

客户端类:

class Client < ActiveRecord::Base
  ## ASSOCIATIONS ##
  belongs_to :user
  has_many :contacts, :dependent => :destroy
  has_many :invoices

  ## ACCESSIBLE ##
  attr_accessible :name, :address_line_one, :address_line_two,
                      :contacts_attributes
  ## NESTED ATTRIBUTES ##
  accepts_nested_attributes_for :contacts

形式:

  = form_for(@client) do |f|
    = f.fields_for(@contact) do |contact|

但我在提交表单时仍然收到此错误:

Can't mass-assign protected attributes: contact

和参数:

"client"=>{"name"=>"23",
 "contact"=>{"name"=>"asdf",
 "email"=>"af@gmail.com"}},
 "commit"=>"Save"}
4

1 回答 1

0

我会做的是:

def new
  @client = Client.new
  @client.contacts.build
  @header = "New Client"

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @client }
  end
end

和:

= form_for(@client) do |f|
  = f.fields_for(:contacts) do |contact|
于 2013-04-20T17:58:01.820 回答