繁体   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