我正在开发一个rails应用程序,我们在DOM中有几个表单实例。看起来像这样:
def edit
// Something here ...
@book = Book.find_by_id(params[:id])
if @book.pages.empty?
(0..3).each do |i|
@book.pages.create
end
end
@pages = @book.pages
end
Rails输出以下形式:
<form method="post" id="edit_page_36" enctype="multipart/form-data" data-remote="true" class="edit_page" autocomplete="off" action="/pages/36" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="patch" name="_method">
<input type="hidden" value="c3DA/d247CSL3mc8aOXNVkQ8D9r5Pfzbm9DYWDRn52g=" name="authenticity_token">
</div>
<input type="hidden" value="36" name="page[id]" id="page_id">
<input type="hidden" name="page[body]" id="page_body">
<input type="hidden" name="page[json]" id="page_json">
<input type="hidden" name="page[background_image]" id="page_background_image">
<input type="hidden" value="1" name="page[page_no]" id="page_page_no">
<input type="hidden" value="21" name="page[book_id]" id="page_book_id">
</form>
和......
<form method="post" id="edit_page_37" enctype="multipart/form-data" data-remote="true" class="edit_page" autocomplete="off" action="/pages/37" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="patch" name="_method">
<input type="hidden" value="c3DA/d247CSL3mc8aOXNVkQ8D9r5Pfzbm9DYWDRn52g=" name="authenticity_token">
</div>
<input type="hidden" value="37" name="page[id]" id="page_id">
<input type="hidden" name="page[body]" id="page_body">
<input type="hidden" name="page[json]" id="page_json">
<input type="hidden" name="page[background_image]" id="page_background_image">
<input type="hidden" value="2" name="page[page_no]" id="page_page_no">
<input type="hidden" value="21" name="page[book_id]" id="page_book_id">
</form>
等等。
您可以看到,hidden_input_fields在每种形式中都具有相同的标识符:id =&#34; page_body&#34;,:id =&#34; page_json&#34;等。根据标准/规范,这个incorrect不是吗?根据我的理解,标识符(:id)应该在DOM中是唯一的,因为它应该识别某些东西。
无论如何,所以不是id =&#34; page_body&#34;我想要的是有轨道输出id =&#34; page_body_36&#34;对于ID为#34; edit_page_36&#34;的表单它的所有属性。
form_for @page是这样的:
= form_for page, :remote => true, :html => { :multipart => true, :autocomplete => 'off'} do |f|
= f.hidden_field :body, :value => page.body
= f.hidden_field :json, :value => page.json
= f.hidden_field :background_image
= f.hidden_field :page_no, :value => page.page_no
= f.hidden_field :book_id, :value => @book.id
任何线索?
答案 0 :(得分:1)
您需要在外部为表单指定id
。更新您的查看表单,如下所示:
= form_for page, :html => { :remote => true, :multipart => true, :autocomplete => 'off'} do |f|
= f.hidden_field :id, :value => page.id, :id => "page_id_#{page.id}" # new field
= f.hidden_field :body, :value => page.body, :id => "page_body_#{page.id}"
= f.hidden_field :json, :value => page.json, :id => "page_json_#{page.id}"
= f.hidden_field :background_image, :id => "page_background_image_#{page.id}"
= f.hidden_field :page_no, :value => page.page_no, :id => "page_page_no_#{page.id}"
= f.hidden_field :book_id, :value => @book.id, :id => "page_book_id_#{page.id}"