简体   繁体   中英

How do I get a nested form working with formtastic?

Whenever I do the following it won't display any of the sub fields on the form:

= f.semantic_fields_for :transfers do |g|

If I singularize it, they display but then the model has no clue what to do with :transfer . I tried to do singular for the accepts_nested_attributes_for to match, but that didn't work either.

Transaction:

has_many :transfers
accepts_nested_attributes_for :transfers

Transfer:

belongs_to :transaction

view:

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

The problem was that I wasn't building a transfer object.

In the controller

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

I hadn't added the second line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM