2017-08-03 16 views
0

私は、billing_informationというモデルユーザーの新しいレコードを作成するためのフォームを作成しようとしています。 Billing_informationには、フォームに含める属性account_nameがあります。デリゲートメソッドを使用しようとしましたが、動作しません。これは、生成: -rails_admin作成フォームに子属性を含める方法

error: unknown attribute 'billing_information_account_name' for User.

class User < ActiveRecord::Base 
    accepts_nested_attributes_for :billing_information 
    has_one :billing_information, inverse_of: :user 
    delegate :account_name, to: :billing_information, allow_nil: true 

    rails_admin do 
     create do 
     field :name 
     field :email 
     field :billing_information_account_name do 
      def value 
       bindings[:object].account_name  
      end 
     end 
     end 
    end 
end 

誰もがよりよい解決策を持っていますか?ありがとうございました。

答えて

0

この場合、悲しいことに、あなたはrails adminの助けを借りることはできませんが、できます。

新しい仮想フィールドを追加し、セッターに入力を処理する必要があります。この例を見てください。

class User < ApplicationRecord 
    has_one :billing_information, inverse_of: :user 

    # A getter used to populate the field value on rails admin 
    def billing_information_account_name 
     billing_information.account_name 
    end 

    # A setter that will be called with whatever the user wrote in your field 
    def billing_information_account_name=(name) 
     billing_information.update(account_name: name) 
    end 

    rails_admin do 
     configure :billing_information_account_name, :text do 
     virtual? 
     end 

     edit do 
     field :billing_information_account_name 
     end 
    end 
    end 

あなたは常にbilling_informationフィールドを追加し、すべての情報を記入するための良いフォームを取得しますつまり、ネストされた属性の戦略を使用して完全なbilling_informationを作成することができます。

関連する問題