English 中文(简体)
验证失败时生成的正平时(n*n)的相同嵌套表单
原标题:Multiple (n) identical nested forms generated square-times(n*n) when validation fails

User has two addresses shipping(:address_type=0) and billing(:address_type=1) User form with 2 classic nested forms for each address type are generated square times every submit and failed validation.

<强 > 模式:

class User < ActiveRecord::Base
    has_many :addresses, :dependent => :destroy
    accepts_nested_attributes_for :addresses
    validates_associated :addresses
end

class Address < ActiveRecord::Base
    belongs_to :user  
    validates :user, :address_type, :first_name, :last_name, :street
end

<强 > 主计长

class UsersController < ApplicationController

public
    def new
        @user = User.new
        @shipping_address = @user.addresses.build({:address_type => 0})
        @billing_address = @user.addresses.build({:address_type => 1})
    end

    def create
        @user = User.new(params[:user])
        if @user.save
            #fine
        else
            render => :new
        end
    end

<强 > 未完成表格

=form_for @user, :html => { :multipart => true } do |ff|
    =ff.fields_for :addresses, @shipping_address do |f|
        =f.hidden_field :address_type, :value => 0

    =ff.fields_for :addresses, @billing_address do |f|
        =f.hidden_field :address_type, :value => 1

    =ff.submit
最佳回答

外表应该像这个样子:

=form_for @user, :html => { :multipart => true } do |ff|
    =ff.fields_for :addresses do |f|

Nothing else. Addressess is already a collection, so you should have just one rendering of it. Also that ":addresses, @shipping_address" makes it to render addresses AND shipping address, even if it s included in @user.addresses.

在新动作中建立的地址将显示在那里, 因为它们在地址收藏中 。

<强度 > EDIT :

如果您仅需要这两个地址, 您可以将地址分类, 并直接传递到字段(_f) :

=form_for @user, :html => { :multipart => true } do |ff|
    =ff.fields_for ff.object.addresses.sort{|a,b| a.address_type <=> b.address_type } do |f|

这样应该可以了

问题回答

惊讶?我想不是,但我是。我发现它是正确的吗?而且它愚蠢而简单。

没有@shipping_address 或@billing_address 当验证失败并再次发布新动作( 表格) 时没有@shipping_ address 或@ billling_ address 。 但是 @user 已经建立 2 个地址, 嵌套形式行为正确, 第一次失败验证两次 。

    def create
        @user = User.new(params[:user])
        if @user.save
            #fine
        else
          @user.addresses.clear
          @user_address = @user.addresses.build({:address_type => 0})
          @user_address.attributes = params[:user][:addresses_attributes]["0"]

          @billing_address = @user.addresses.build({:address_type => 1})
          @billing_address.attributes = params[:user][:addresses_attributes]["1"]
          render => :new
        end
    end




相关问题
Rails checkout form with multiple models

I m creating a shopping cart based on the one in Agile Web Development With Rails (version 3). I have it set up where "items" are added to a "cart", then upon starting the checkout process, they are ...

Rails nested forms

I would like to create form with text_fields TITLE CONTENT TAGS I have Post (TITLE, CONTENT) and Tag (TAGS) model. TAGS is a single text field. What do I have to do to save TAGS to Tag model. Let ...

is there better way to count for three level nested forms

I have a nested forms like: class House < ActiveRecord::Base has_many :rooms accepts_nested_attributes_for :rooms attr_accessible :rooms_attributes end class Room < ActiveRecord::Base ...

Multi-Model Forms with Image Upload

I m trying to create a poll object which has many answers. Each answer can have an image attached. How can I create a form that allows me to enter the question and add a variable number of answers (...

Ruby Rails Nested Records

I m not sure if what I m even trying to do is possible but here goes. I have an SQL database with the following tables defined (showing only relevant tables in SQL): CREATE TABLE customers( id ...

热门标签