code_junkie

Nesting content tags in rails

Nov 13th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. # from tagged_builder.rb, included as a FormBuilder helper
  2. def self.create_tagged_field(method_name)
  3. define_method(method_name) do |label, *args|
  4. @template.content_tag("p",
  5. @template.content_tag("label" ,
  6. label.to_s.humanize.capitalize,
  7. :for => "#{@object_name}_#{label}")
  8. +
  9. super)
  10. end
  11. end
  12.  
  13. <p><label>Name
  14. <span class="small">Add your name</span>
  15. </label>
  16. <input type="text" name="textfield" id="textfield" />
  17.  
  18. <p><label for="object_name">Name</label></p>
  19.  
  20. def self.create_tagged_field(method_name)
  21. define_method(method_name) do |label, warning, *args|
  22. @template.content_tag("p",
  23. @template.content_tag("label" ,
  24. label.to_s.humanize.capitalize+
  25. content_tag("span", warning),
  26. :for => "#{@object_name}_#{label}")
  27. +
  28. super)
  29. end
  30. end
  31.  
  32. def self.create_tagged_field(method_name)
  33. define_method(method_name) do |label, *args|
  34. # accepts the warning hash from text_field helper
  35. if (args.first.instance_of? Hash) && (args.first.keys.include? :warning)
  36. warning = args.first[:warning]
  37. end
  38. @template.content_tag("label" , label.to_s.humanize+(@template.content_tag("span", warning, :class =>'small')),
  39. :for => "#{@object_name}_#{label}") +
  40. super
  41. end
  42. end
  43.  
  44. def self.create_tagged_field(method_name)
  45. define_method(method_name) do |label, *args|
  46. @template.content_tag("p",
  47. @template.content_tag("label" , labels.to_s.humanize+(@template.content_tag("span", args[:warning]) unless !args[:warning]), :for => "#{@object_name}_#{label}") +
  48. super)
  49. end
  50. end
  51.  
  52. <%= form.text_field :name, :warning => "dont do that"] %>
  53.  
  54. Symbol as array index
Add Comment
Please, Sign In to add comment