如何获得使用formtastic的嵌套表单?

时间:2012-02-14 19:06:57

标签: ruby-on-rails ruby ruby-on-rails-3 formtastic

每当我执行以下操作时,它都不会显示表单上的任何子字段:

= f.semantic_fields_for :transfers do |g|

如果我将它单一化,它们会显示但是模型不知道如何处理:transfer。我试图为accepts_nested_attributes_for匹配做单数,但这也不起作用。

交易:

has_many :transfers
accepts_nested_attributes_for :transfers

传输:

belongs_to :transaction

视图:

= semantic_form_for [@user, @transaction], url: url, style: "width: inherit;" do |f|
  = f.semantic_fields_for :transfers do |g|
    = g.inputs do
      = g.input :amount
      %li
        %label
        = "(#{Transfer::TRANSFER_FEE.format :symbol} transaction fee will be added)"
    = g.inputs do
      %li
        %label
        Click
        %input{ type: "image", value: "submit", style: "vertical-align: middle;",
          src: "https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" }
        or fill in your credit card information below
    = g.inputs do
      = g.input :first_name
      = g.input :last_name
      = g.input :card_type, as: :select, collection: [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]]
      = g.input :card_number
      = g.input :card_verification
      = g.input :card_expires_month, as: :select, collection: (1..12)
      - year = Time.now.year
      = g.input :card_expires_year, as: :select, collection: (year..(year+25))
  = f.inputs do
    %li
      %label
      %input{ type: "submit", value: "Buy", name: "use_cc" }

1 个答案:

答案 0 :(得分:0)

问题是我没有建立转移对象。

在控制器中

@transaction = current_user.transactions.build
@transaction.transfers.build

我没有添加第二行。