簡體   English   中英

嵌套屬性預填充子數據與空父

[英]nested attributes prefilled child data with empty parent

我陷入了需要使用空表單為 fields_for 提供預填充數據的情況。 讓我用一個例子來解釋

協會

class User < ApplicationRecord
  has_one :account, foreign_key: :user_id

  accepts_nested_attributes_for :account
end

Class Account
  belongs_to :user
end

現在我在儀表板索引頁面上有一個表單。

<%= form_for @account, :url => account_path, html: { class: "abc" } do |f| %>
  <%= f.hidden_field :user_id, :value => current_user.id %>
  <%= f.fields_for :user do |user| %>
    <div class="medium-4 columns">
      <label>First Name</label>
      <%= user.text_field :first_name , class: 'xyz', data: {input: 'someinput'} %>
    </div>
    <div class="medium-4 columns">
      <label><b>Last Name<span class="invalid_message"> This field is required</span></b></label>
      <%= user.text_field :last_name, class: 'xyz', data: {input: 'someinput'} %>
    </div>
  <% end %>

  <div class="medium-4 medium-offset-2 columns">
    <label>Phone Number</label>
    <%= f.text_field :phone_number, class: 'xyz', data: {input: 'someinput'} %> 
  </div>
<% end %>

Controller

 class AccountsController < ApplicationController

      def create
        @account = Account.new(account_params)
        if @account.save
          render json: {status: 'successfull'}
        else
          render json: {error: "#{@account.errors.full_messages}"}, status: 400
        end
      end

      private
      def account_params
        params.require(:account).permit(:phone_number, :user_id, user_attributes: [:first_name, :last_name])
      end
    end

    class DashboardController < ApplicationController

      def index
        ##I will always have current_user. For the account it will be blank for the first time
        if current_user.account.blank?
          @account = Account.new
        else
          @account = current_user.account
        end
      end
    end

參數

{"utf8"=>"✓", "authenticity_token"=>"asdasdadadadadsadadadadadQpy0tA82asdaalAgJsUcNk1i/kGETfZqnuQA==", "account"=>{"user_id"=>"123", "user"=>{"first_name"=>"sd", "last_name"=>"ad"}, "phone_number"=>"1212"}}

有兩個問題
1) 名字和姓氏沒有被預填
2) 參數運行不正確。 它應該是 account_attributes 而不是參數中的 account。

注意:在我的情況下,@account 第一次為空,但用戶對象(即當前用戶)仍然具有 first_name 和 last_name,我需要預先填寫 fields_for。 另外,我需要一種方法來更新名字和姓氏

誰能告訴我哪里做錯了

@account = Account.new更改為@account = current_user.build_account 您應該會看到預先填寫的字段。

最后,我找到了解決方案。 我做錯了幾件事

1) 在 form_for 中應該是 @user 而不是 @account
2)然后在 controller 中,此表單將始終將其發送到更新操作而不是創建。 原因是我將始終擁有 current_user,因此 rails 會自動檢查對象(current_user)是否存在,因此不會發送到創建,而是發送到更新操作
3) 最后,在使用一對一關聯時,我們需要加載父 object 並構建子 object。

<%= form_for @user, :url => user_path, html: { class: "abc" } do |f| %>
    <div class="medium-4 columns">
      <label>First Name</label>
      <%= f.text_field :first_name , class: 'xyz', data: {input: 'someinput'} %>
    </div>
    <div class="medium-4 columns">
      <label><b>Last Name<span class="invalid_message"> This field is required</span></b></label>
      <%= f.text_field :last_name, class: 'xyz', data: {input: 'someinput'} %>
    </div>
  <%= f.fields_for :account do |account_info| %>
    <div class="medium-4 medium-offset-2 columns">
      <label>Phone Number</label>
    <%= account_info.text_field :phone_number, class: 'xyz', data: {input: 'someinput'} %> 
    </div> 
  <% end %>
<% end %>


class UsersController < ApplicationController

      def update
        if current_user.update(account_params)
          render json: {status: 'successfull'}
        else
          render json: {error: "#{@account.errors.full_messages}"}, status: 400
        end
      end

      private
      def account_params
        params.require(:account).permit(:first_name, :last_name, account_attributes: [:phone_number])
      end
    end

    class DashboardController < ApplicationController

      def index
        ##I will always have current_user. For the account it will be blank for the first time
        if current_user.account.blank?
           @account = current_user
           @account.build_account
        end
      end
    end

暫無
暫無

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

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