[英]Rails render partial dynamically
我得到這個有點瘋狂,我有誰HAS_MANY 服務員一個事件 ,但我一直沒能到事件表單中動態創建,我有一個部分與助理的形式 ,這個工程:
<%= form.fields_for :attendants do |form_attendant| %>
<%= render 'form_attendant', form_attendant: form_attendant %>
<% end %>
對於1位服務員,如果我將其復制粘貼,則可用於2位,依此類推。
我想要一個按鈕來幫我做,我已經閱讀並嘗試了loooot。
選項1:我在js中嘗試通過以下方式將其附加:
$("#attendants").append "<%= form.fie ... %>"
但是它只是打印文本而不會渲染部分文本,我也嘗試過使用escape_javascript ,但是沒有運氣。
我現在正在嘗試:
<%= link_to "Agregar", new_attendant_path, remote: true %>
我在attants / new.js.erb中有這些
$('#attendants').append("<%= escape_javascript ( render partial: 'form_attendants' ) %>");
哪個可以正常工作並渲染部分內容,但是現在我不知道如何發送本地事件表單,因此我可以使<%= form.fields_for :attendants do |form_attendant| %> ...
<%= form.fields_for :attendants do |form_attendant| %> ...
在部分內,
任何幫助將不勝感激,我幾乎要哭了
嘗試nested_form_fields gem
= form_for @user do |f|
= f.nested_fields_for :videos do |ff|
= ff.remove_nested_fields_link
= ff.text_field :video_title
..
= f.add_nested_fields_link :videos
您可以更改remove_nested_fields_link和add_nested_fields_link的鏈接文本,添加類/屬性等。
您還可以通過覆蓋設置來自定義添加/刪除功能
將nested_form_fields.js.coffee插入Rails JavaScript資產
了解更多信息
https://github.com/ncri/nested_form_fields
<%= form_for @users do |f| %>
<%= f.fields_for :attendants do |form_attendant| %>
<%= render 'form_attendant', form_attendant: form_attendant %>
<% end %>
<%= f.add_nested_fields_link :attendants %>
<% end %>
部分_form_attendant.html.erb
<%= form_attendant.remove_nested_fields_link %>
<%= form_attendant.text_field :attd_date %>
<%= form_attendant.text_field :attd_time %>
嘗試使用繭寶石 ,它將做所有這些事情
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.