【问题标题】:nested form params in rails with simple formRails 中的嵌套形式参数,形式简单
【发布时间】:2012-12-02 01:59:39
【问题描述】:

我正在尝试让嵌套表单在一个表单中创建用户与其地址之间的关联。

我看到的问题是,每当验证失败时,内部地址字段表单不会重新填充或显示验证错误。

这是表格:

<%= simple_form_for(@user, :url => register_and_checkout_path, :html => {:class => "form-horizontal clearfix"}) do |f| %>
<p>New Member &amp; Guest</p>
<small>Mandatory fields marked*</small>
     <%= f.error_notification %>
     <%= f.input :is_checkout, :as => :hidden, :input_html => { :value => true } %>
      <%= f.input :first_name, :input_html => {:class => "input-xlarge"} %>
      <%= f.input :last_name, :input_html => {:class => "input-xlarge"} %>
      <%= f.input :email, :input_html => {:class => "input-xlarge", :placeholder => "you@youremail.com"} %>

     <%= simple_fields_for (:address) do |a| %>
        <%= a.input :phone1, :label => "Contact number", :input_html => {:class => "input-xlarge"}  %>
        <p class="uppercase">Your Address</p>
        <%= a.input :address_1, :label => "Street Address", :input_html => {:class => "input-xlarge"}  %>
        <%= a.input :address_2, :label => false, :input_html => {:class => "input-xlarge"}  %>
        <%= a.input :address_3, :label => false, :input_html => {:class => "input-xlarge"}  %>
        <%= a.input :suburb, :input_html => {:class => "input-xlarge"}  %>
        <%= a.input :state, :input_html => {:class => "input-xlarge"}  %>
        <%= a.input :postcode, :label => "Postcode/zipcode", :input_html => {:class => "input-xlarge"}  %>
        <%= a.input :country, :priority => [ "Australia", "New Zealand", "Unites States", "United Kingdom" ] %>

     <% end %>
        <p>Become a valued member</p>
        <%= f.input :password, :input_html => {:class => "input-xlarge"}  %>
        <%= content_tag(:button, :class => 'btn btn-large btn-success floatright') do %> <%= content_tag :i, '', :class => 'icon-white icon-shopping-cart' %> Register and Checkout <% end %>
     <% end %>
</div>

我尝试读取参数(这是大订单控制器创建方法的一部分)

if params[:is_register]
      Rails.logger.debug {"Registering before checking out"}
      @user = Member::User.new(params[:member_user])
      @user.address = Member::Address.new(params[:address])
      Rails.logger.debug {"address params " +params[:address].to_yaml }
      @user.pending_order = Checkout::Order.new()
      @user.pending_order.status = 'P'
      Rails.logger.debug {"Saving the new user record " + @user.to_yaml}
      Rails.logger.debug {"Saving the new user address record " + @user.address.to_yaml}
    end

    Rails.logger.debug {"Value of user " + @user.to_yaml}

    respond_to do |format|
      if params[:is_login] and user
        format.html { render :shipping_billing }
        format.json { render json: @order, status: :created, location: @order }
      elsif params[:is_register] and @user and @user.save and @user.address.save
        Rails.logger.debug {"Saved new customer account and address"}
        format.html { render :shipping_billing, :notice => "Your account was successfully registered"}
        format.json { render json: @order}
      else
        format.html { render action: "new", :notice => "Could not log in" }
        format.json { render json: @checkout_order.errors, status: :unprocessable_entity }
      end
    end

最后是相关模型:

class Member::User < ActiveRecord::Base
  authenticates_with_sorcery!
  # attr_accessible :title, :body

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :address, :address_attributes, :first_name, :last_name, :order_attributes, :pending_order, :is_checkout

  attr_accessor :is_checkout


  # Orders and Order History
  has_many :orders, :class_name => "Checkout::Order"
  has_one  :pending_order,
           :class_name => "Checkout::Order",
           :conditions => ['status = ?', 'P']


  has_many :addresses, :class_name => "Member::Address"
  has_one  :primary_billing_address,
           :class_name => "Member::Address",
           :conditions => ['is_primary_billing = ?',true]
  has_one  :primary_shipping_address,
           :class_name => "Member::Address",
           :conditions => ['is_primary_shipping = ?',true]
  has_one :address,
          :class_name => "Member::Address",
          :conditions => ['is_primary_billing = ?', true]

  accepts_nested_attributes_for  :address
  accepts_nested_attributes_for  :pending_order

  validates_presence_of :email, :password, :first_name, :last_name
  validates_confirmation_of :password
  validates_uniqueness_of :email

end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    对于正在寻找这个问题的答案的其他人,我想通了,这是一个非常简单的错误,经过所有时间试图弄清楚它是表格中的这一行。

    <%= f.simple_fields_for :address do |a| %>
    

    请注意,容器表单构建器现在引用了 simple_fields_for。

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2014-08-14
      • 2023-02-17
      • 1970-01-01
      相关资源
      最近更新 更多