Ruby on Rails/示例/HTML4.01
外观
< Ruby on Rails | 示例
Rails 默认生成 XHTML。 它可以被设置为生成 HTML4.01,可以使用W3C HTML 验证器进行验证。
布局以 HTML4.01 Strict DOCTYPE 开头
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
application_helper.rb
看起来像这样
module Html4ize
# Supports making alternative versions of Rails helpers that output HTML
# 4.01 markup as opposed to XHTML. Example:
#
# html4_version_of :submit_tag
#
# ..which creates a submit_tag_html4 helper that takes the same options as
# submit_tag but which renders HTML 4.01.
#
def html4_version_of helper_name
define_method "#{helper_name}_html4" do |*args|
html = send helper_name, *args
html.gsub! " />", ">"
html
end
end
def html4_versions_of *helper_names
helper_names.each do |helper_name|
html4_version_of helper_name
end
end
class << self
def included receiver
receiver.extend Html4ize
end
end
end
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
include Html4ize
html4_versions_of :stylesheet_link_tag, :image_tag, :text_field, :submit_tag
...
end
# Extend FormBuilder with our own helper methods
class ActionView::Helpers::FormBuilder
include Html4ize
html4_versions_of :text_field, :check_box, :hidden_field, :submit
...
end
视图可以引用新包装的助手
...
<%= image_tag_html4 "logo.png" %>
...
<% form_for :person do |form| %>
<div><b>Name:</b><%= form.text_field_html4 :name %></div>
...
<div class="submit"><%= form.submit_html4 "Save" %></div>
<% end %>