- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (7)
- 7.1.3.4 (0)
- What's this?
Action View Record Identifier
RecordIdentifier encapsulates methods used by various ActionView helpers to associate records with DOM elements.
Consider for example the following code that form of post:
<%= form_for(post) do |f| %> <%= f.text_field :body %> <% end %>
When post is a new, unsaved ActiveRecord::Base instance, the resulting HTML is:
<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>
When post is a persisted ActiveRecord::Base instance, the resulting HTML is:
<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>
In both cases, the id and class of the wrapping DOM element are automatically generated, following naming conventions encapsulated by the RecordIdentifier methods #dom_id and #dom_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"
Note that these methods do not strictly require Post to be a subclass of ActiveRecord::Base. Any Post class will work as long as its instances respond to to_key and model_name, given that model_name responds to param_key. For instance:
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
Constants
NEW = "new"
JOIN = "_"