簡體   English   中英

Rails Ajax將其他表單字段插入表單

[英]Rails ajax insert additional form fields into form

我試圖通過內置的Rails Ajax方法向表單中添加一個新的選擇字段,但不確定如何繼續。 我已經看過其他的ajax rails,但是如果有人發現了一個更相關的指南,這會有所幫助。

details.html.erb

<%= form_for @event, url: wizard_path do |f| %>
# lots of forms here

    <div id="add-field">

    </div>
    <%= link_to "Add Event Detail", add_event_detail_path, remote: true %>

    <%= f.submit "Next" %>
<% end %>

events / build_controller添加了此

def add_event_detail
    respond_to do |format|
        format.js
    end
end

add_event_detail.erb

$("#add-field").append("<%= f.select(:event_detail, options_for_select([['Option1', 'Option1']) %> <%= f.text_field :event_detail %> ");

我現在也添加了這條路線

match "/add_event_detail" => "events/build#add_event_detail", via: 'get'

我有幾個問題(除了可能看到的錯誤代碼之外)。 如何將f變量傳遞到jquery append方法中,以及從本質上來說,這是處理每個新創建的表單的唯一名稱的好方法:event_detail1 :event_detail2等。理想情況下,我想將該名稱鏈接到形成

例如:location為選項:location

如果您嘗試查看nested_form gem ie( https://github.com/ncri/nested_form_fields ),它可以將字段動態添加到表單中。 或嘗試查看RailsCasts#403動態表格,它將為您提供明確的方向。 希望這可以幫助您朝正確的方向發展。

暫無
暫無

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

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