John Merlino
unread,Sep 9, 2012, 12:49:09 AM9/9/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ruby on Rails: Talk
Without going to detail, I'll just show important code:
text field is passed the name of object, could be a symbol (:user) or
string ("user[]"). method is datbase attribute or virtual attribute.
the options hash, among other things, includes the object if persisted
(e.g. @user).
def text_field(object_name, method, options = {})
Tags::TextField.new(object_name, method, self, options).render
end
TextField doesn't have initialize, it calls on super which is Base,
and we create some instance variables holding our object information
and so forth:
class Base
...
def initialize(object_name, method_name, template_object,
options = {})
@object_name, @method_name = object_name.to_s.dup,
method_name.to_s.dup
@template_object = template_object
@object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]
$/,"]")
@object = retrieve_object(options.delete(:object))
@options = options
...
We call render on text field object
def render
options = @options.stringify_keys
options["size"] = options["maxlength"] unless options.key?
("size")
options["type"] ||= field_type
options["value"] = options.fetch("value")
{ value_before_type_cast(object) } unless field_type == "file"
options["value"] &&= ERB::Util.html_escape(options["value"])
add_default_name_and_id(options)
tag("input", options)
end
def tag(name, options = nil, open = false, escape = true)
"<#{name}#{tag_options(options, escape) if options}#{open ?
">" : " />"}".html_safe
end
and thats pretty much it. Wheres the name attribute and its value
appended to the returned string from render?