嵌套形式和 link_to_function
我有一个像这样的嵌套表单:
<% form_for setup_training(@training), :url => admin_trainings_path, :html => { :class => :form } do |f| -%>
<!-- begin of form -->
<!-- content of :partial => "day_form" -->
<% f.fields_for :days do |days_form| %>
<%= render :partial => "admin/days/form_inner", :locals => { :f => days_form }%>
<% end %>
<!-- end of content of :partial => "day_form" -->
<%= link_to_function("[+] Ajouter une date", nil, :id => "add-place-and-dates-link") do |page|
page.insert_html :bottom, "place-and-dates-inner", :partial => "day_form", :locals => { :f => f }
end %>
<% end -%>
第一次加载页面时,第一个 fields_for 块的 id 为 0;
第一次单击“添加日期”时,会创建一个新的 fields_for 块,其 id = 1;
然后,每次我点击“添加日期”时,都会显示一个新的 fields_for 块,但 id 保持为 1。
HTML 示例可能更明确,这是我得到的输出:
<!-- This part is generated when i display /new, id is 0 -->
<label class="label" for="training_days_attributes_0_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_0_place_id" name="training[days_attributes][0][place_id]" size="30" type="text">
<!-- This part is generated when i click on "add a new date", id is 1 -->
<label class="label" for="training_days_attributes_1_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_1_place_id" name="training[days_attributes][1][place_id]" size="30" type="text">
<!-- This part is generated when i click on "add a new date" a second time, id is 1, but it should be 2 -->
<label class="label" for="training_days_attributes_1_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_1_place_id" name="training[days_attributes][1][place_id]" size="30" type="text">
提前感谢您的帮助!
I've got a nested form like this :
<% form_for setup_training(@training), :url => admin_trainings_path, :html => { :class => :form } do |f| -%>
<!-- begin of form -->
<!-- content of :partial => "day_form" -->
<% f.fields_for :days do |days_form| %>
<%= render :partial => "admin/days/form_inner", :locals => { :f => days_form }%>
<% end %>
<!-- end of content of :partial => "day_form" -->
<%= link_to_function("[+] Ajouter une date", nil, :id => "add-place-and-dates-link") do |page|
page.insert_html :bottom, "place-and-dates-inner", :partial => "day_form", :locals => { :f => f }
end %>
<% end -%>
The first time i load my page, the first fields_for block has id 0;
The first time i click on "add a date", a new fields_for block is created with id = 1;
Then, everytime i clink on "add a date", a new fields_for block is displayed, but the id stays to 1.
An HTML example may be more explicit, here's the output i've got :
<!-- This part is generated when i display /new, id is 0 -->
<label class="label" for="training_days_attributes_0_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_0_place_id" name="training[days_attributes][0][place_id]" size="30" type="text">
<!-- This part is generated when i click on "add a new date", id is 1 -->
<label class="label" for="training_days_attributes_1_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_1_place_id" name="training[days_attributes][1][place_id]" size="30" type="text">
<!-- This part is generated when i click on "add a new date" a second time, id is 1, but it should be 2 -->
<label class="label" for="training_days_attributes_1_place_id">Lieu</label>
<input class="text_field" id="training_days_attributes_1_place_id" name="training[days_attributes][1][place_id]" size="30" type="text">
Thank you per advance for you help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看页面源中的内联 JavaScript,您会发现您的 link_to_function 包含静态 html,它将被插入到 place-and-dates-inner 容器中。这不会改变,因为它是在服务器上构建并发送给用户的。
要解决此问题,您可能最好在客户端计算之前插入地点和日期内部的容器数量,并在模板中相应增加。老实说,我一直不喜欢这种内联 link_to_function 的东西。
If you have a look in your inline javascript in your page source you'll see that your link_to_function contains static html which will be be inserted into the place-and-dates-inner container. This does not change because it was build on the server and sent to the user.
To fix this you probably would be best to count client side the number of containers that have previously been inserted into the place-and-dates-inner and increment accordingly in your template. This kind of inline link_to_function stuff I always disliked to be honest.