優(yōu)化網(wǎng)址的選擇:
- 方案一:網(wǎng)址上除了數(shù)字ID,可以再加上文字
def to_param
"#{self.id}-#{self.name}"
end
- 方案二:不要用數(shù)據(jù)庫的遞增數(shù)字ID,而是用一個亂數(shù)產(chǎn)生的 ID
friendly_id
- 方案三:除了用亂數(shù)ID,也可以讓用戶自定義 ID
<div class="form-group">
<%= f.label :friendly_id %>
<%= f.text_field :friendly_id, :required => true, :class => "form-control" %>
<p class="help-block">限小寫英數(shù)字及橫線,將作為網(wǎng)址的一部分</p>
</div>
翻譯i18n
config.i18n.default_locale = "zh-CN"
<h1><%= t("admin.event_list") %></h1>
也可以寫成t("event_list", :scope => "admin")
結(jié)果是一樣的。其中 t 等同于 I18n.t,是個 Helper 方法,會根據(jù)語系來做字符串的替換。
詞匯中內(nèi)嵌變量 %{variable_name}
"zh-CN":
hello: "親~ %{name} 你好:"
<%= t( "hello", :name => current_user.display_name) %>
語系靜態(tài)樣板get "/faq" => "pages#faq"
新增 app/views/pages/faq.zh-CN.html.erb
新增 app/views/pages/faq.en.html.erb