简体   繁体   English

嵌套属性和belongs_to关联

[英]Nested attributes and belongs_to association

Rails 4.2.8 导轨4.2.8

user.rb user.rb

class User < ActiveRecord::Base
   devise :database_authenticatable, :registerable, :rememberable,
         :validatable, :encryptable, :omniauthable, omniauth_providers: [:facebook], encryptor: :restful_authentication_sha1
   attr_accessible :email, :name, :password, :password_confirmation, :is_admin, :is_master

   has_one :customer
   accepts_nested_attributes_for :customer
end

customer.rb customer.rb

class Customer < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
end

customers_contoller.rb customer_contoller.rb

class CustomersController < ApplicationController
  def edit
    @customer = current_user.customer
  end

  def update
    @customer = current_user.customer
    @customer.update customer_params
    render 'edit'
  end

  private

  def customer_params
    params.require(:customer).permit(:first_name, :last_name, :phone, user_attributes: [:email, :password, :password_confirmation])
  end
end

customers/edit.html.erb customer / edit.html.erb

<%= form_for @customer, html: { class: 'checkout-attributes profile-form' } do |f| %>
  <div class="row">
    <div class="col-sm-6">
      <span class="help-block">First Name</span>
      <%= f.text_field :first_name, class: 'form-control' %>
    </div>
    <div class="col-sm-6">
      <span class="help-block">Last Name</span>
      <%= f.text_field :last_name, class: 'form-control' %>
    </div>

    <div class="col-sm-12">
      <span class="help-block">Email</span>
      <%= fields_for :user, @customer.user do |u| %>
      <%= u.email_field :email, class: 'form-control' %>
      <% end %>
    </div>

    <div class="col-sm-12">
      <span class="help-block">Phone Number</span>
      <%= f.text_field :phone, class: 'form-control' %>
    </div>
    <div class="col-sm-12">
      <span class="help-block">Password</span>
      <%= fields_for :user, @customer.user do |u| %>
      <%= u.password_field :password, class: 'form-control' %>
      <% end %>
    </div>
    <div class="col-sm-12">
      <span class="help-block">Confirm Password</span>
      <%= fields_for :user, @customer.user do |u| %>
      <%= u.password_field :password_confirmation, class: 'form-control' %>
      <% end %>                      
    </div>
  </div>
<% end %>

That's what I see in log 这就是我在日志中看到的

Started PATCH "/customers/560738" for 127.0.0.1 at 2015-11-04 08:15:20-0500 Processing by CustomersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OL7NCIMKCb+LQZ+1voahUsNyD37q6sal8W7HsV4EKKP9ABMuDVGqxs0nFS2Cyo7A6XBkRLYv9tjTYH4kaHNdkA==", "image"=>"", "customer"=>{"first_name"=>"Jack", "last_name"=>"Drobazko", "phone"=>"5084427293"}, "user"=>{"email"=>"ddd@fff.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "id"=>"560738"}

Also I see that customer_params methods gives filtered (without user part) hash {"first_name"=>"Jack", "last_name"=>"Drobazko", "phone"=>"5084427293"} 我也看到customer_params方法提供了经过过滤的(没有用户部分)哈希值{“ first_name” =>“ Jack”,“ last_name” =>“ Drobazko”,“ phone” =>“ 5084427293”}

So how to make it works? 那么如何使其工作呢?

According to your log file: 根据您的日志文件:

"image"=>"", "customer"=>{"first_name"=>"Jack", "last_name"=>"Drobazko", "phone"=>"5084427293"}, "user"=>{"email"=>"ddd@fff.com", "password"=>"[FILTERED]", "password_confirmation"=>" ... “ image” =>“”,“ customer” => {“ first_name” =>“ Jack”,“ last_name” =>“ Drobazko”,“ phone” =>“ 5084427293”}, “ user” => {“电子邮件“ =>” ddd@fff.com“,” password“ =>” [已过滤]“,” password_confirmation“ =>” ...

All your fields_for inputs outside of a main form. 您所有的fields_for输入都在主窗体之外。 Fix your form to wrap all user input in the fields_for helper. 修复您的表单,以将所有用户输入包装在fields_for帮助器中。

Note about f.fields_for and simple fields_for , it's very important 注意有关f.fields_for和simple fields_for这非常重要

<%= form_for @customer, html: { class: 'checkout-attributes profile-form' } do |f| %>   
  # some code here  
      <%= f.fields_for :user, @customer.user do |u| %>
        <%= u.email_field :email, class: 'form-control' %>
        <%= u.password_field :password, class: 'form-control' %>
        <%= u.password_field :password_confirmation, class: 'form-control' %>
      <% end %>
    </div>
  # some code here

Here is wonderful article from Rails core team, about using nested attributes . 这是Rails核心团队的精彩文章,内容涉及使用nested attributes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM