跳到內容 跳到搜尋

Action View 記錄識別碼

RecordIdentifier 封裝 ActionView 各種輔助程式使用的各種方法,用於將記錄與 DOM 元素關聯。

例如,考慮以下表單的程式碼

<%= form_with(model: post) do |f| %>
  <%= f.text_field :body %>
<% end %>

post 是新的,未儲存的 ActiveRecord::Base 執行個體時,產生的 HTML 如下所示

<form class="new_post" id="new_post" action="/posts" accept-charset="UTF-8" method="post">
  <input type="text" name="post[body]" id="post_body" />
</form>

post 是已持續 ActiveRecord::Base 執行個體時,產生的 HTML 如下所示

<form class="edit_post" id="edit_post_42" action="/posts/42" accept-charset="UTF-8" method="post">
  <input type="text" value="What a wonderful world!" name="post[body]" id="post_body" />
</form>

在兩種情況下,包裝 DOM 元素的 idclass 會自動產生,並遵循 RecordIdentifier 方法 dom_iddom_class 所封裝的命名慣例。

dom_id(Post)             # => "new_post"
dom_class(Post)          # => "post"
dom_id(Post.new)         # => "new_post"
dom_class(Post.new)      # => "post"
dom_id(Post.find 42)     # => "post_42"
dom_class(Post.find 42)  # => "post"

請注意,這些方法並不要求 Post 嚴格來說是 ActiveRecord::Base 的子類別。只要它的執行個體回應 to_keymodel_name,且 model_name 回應 param_key,任何 Post 類別都可以使用。例如

class Post
  attr_accessor :to_key

  def model_name
    OpenStruct.new param_key: 'post'
  end

  def self.find(id)
    new.tap { |post| post.to_key = [id] }
  end
end
方法
D
R

常數

JOIN = "_"
 
NEW = "new"
 

執行個體公開方法

dom_class(record_or_class, prefix = nil)

DOM 類別慣例是使用物件或類別的單數形式。

dom_class(post)   # => "post"
dom_class(Person) # => "person"

如果你需要在同一個檢視中對同一個類別的多個執行個體進行處理,你可以前置 dom_class

dom_class(post, :edit)   # => "edit_post"
dom_class(Person, :edit) # => "edit_person"
# File actionview/lib/action_view/record_identifier.rb, line 78
def dom_class(record_or_class, prefix = nil)
  singular = model_name_from_record_or_class(record_or_class).param_key
  prefix ? "#{prefix}#{JOIN}#{singular}" : singular
end

dom_id(record_or_class, prefix = nil)

DOM id 慣例是使用物件或類別的單數形式,且 id 在底線之後。如果找不到 id,請使用 “new_” 取而代之。

dom_id(Post.find(45)) # => "post_45"
dom_id(Post)          # => "new_post"

如果你需要在同一個檢視中對同一個類別的多個執行個體進行處理,你可以前置 dom_id

dom_id(Post.find(45), :edit) # => "edit_post_45"
dom_id(Post, :custom)        # => "custom_post"
# File actionview/lib/action_view/record_identifier.rb, line 93
def dom_id(record_or_class, prefix = nil)
  raise ArgumentError, "dom_id must be passed a record_or_class as the first argument, you passed #{record_or_class.inspect}" unless record_or_class

  record_id = record_key_for_dom_id(record_or_class) unless record_or_class.is_a?(Class)
  if record_id
    "#{dom_class(record_or_class, prefix)}#{JOIN}#{record_id}"
  else
    dom_class(record_or_class, prefix || NEW)
  end
end

執行個體私有方法

record_key_for_dom_id(record)

傳回適合用於 HTML DOM id 的金鑰屬性字串表示方式。如果想要,可以覆寫自訂預設產生的字串表示方式。如果您想從 dom_id 讀回金鑰以便查詢基礎資料庫記錄,您應寫入輔助程式,例如「person_record_from_dom_id」,它將根據預設實作(只使用「_」加入所有金鑰屬性)或您覆寫的方法版本萃取金鑰。預設情況下,此實作會透過方法將金鑰字串傳遞出去,該方法會將 DOM id 中所有無效字元換成有效字元。如果您覆寫此方法,您需要確保您的 dom id 有效。

# File actionview/lib/action_view/record_identifier.rb, line 113
def record_key_for_dom_id(record) # :doc:
  key = convert_to_model(record).to_key
  key && key.all? ? key.join(JOIN) : nil
end