【问题标题】:How to set unique_ids on form attributes for multiple forms in a DOM?如何在 DOM 中的多个表单的表单属性上设置 unique_ids?
【发布时间】:2014-07-31 15:00:45
【问题描述】:

我正在开发一个 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 = "page_body"、:id = "page_json" 等。这不是标准/规范中的incorrect 吗?据我了解,标识符 (:id) 在整个 DOM 中应该是唯一的,因为它应该识别某些东西。

无论如何,所以我想要的不是 id = "page_body",而是让 rails 输出 id = "page_body_36" 用于所有属性为 id "edit_page_36" 的表单。

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

有什么线索吗?

【问题讨论】:

  • 如果您能向我们展示您的观点将会很有帮助

标签: ruby-on-rails forms ruby-on-rails-4


【解决方案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}"

【讨论】:

  • 这将为表单设置唯一 ID,而不是表单中包含的字段。我认为 dom_id 方法在这里会有所帮助,但即使这样似乎也不起作用。至少到目前为止。
  • 看看更新的答案..如果我知道您希望每个元素都有唯一的 ID..
  • 是的,谢谢!我看到的唯一问题是大量重复代码来做一件事。是不是很丑?
  • 同意,那你可以试试js。它会将动态 ID 添加到您的字段中。但是,这并不可取,因为它会在 DOM 和 js 加载后更新 ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 2023-03-16
  • 2020-12-20
相关资源
最近更新 更多