簡體   English   中英

嵌套屬性和belongs_to關聯

[英]Nested attributes and belongs_to association

導軌4.2.8

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

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

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

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 %>

這就是我在日志中看到的

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"}

我也看到customer_params方法提供了經過過濾的(沒有用戶部分)哈希值{“ first_name” =>“ Jack”,“ last_name” =>“ Drobazko”,“ phone” =>“ 5084427293”}

那么如何使其工作呢?

根據您的日志文件:

“ image” =>“”,“ customer” => {“ first_name” =>“ Jack”,“ last_name” =>“ Drobazko”,“ phone” =>“ 5084427293”}, “ user” => {“電子郵件“ =>” ddd@fff.com“,” password“ =>” [已過濾]“,” password_confirmation“ =>” ...

您所有的fields_for輸入都在主窗體之外。 修復您的表單,以將所有用戶輸入包裝在fields_for幫助器中。

注意有關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

這是Rails核心團隊的精彩文章,內容涉及使用nested attributes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM