Static pages can sometimes be a little awkward to add to a Rails app. See a couple different solutions to this problem in this episode.
# pages_controller.rb
def show
if params[:permalink]
@page = Page.find_by_permalink(params[:permalink])
raise ActiveRecord::RecordNotFound, "Page not found" if @page.nil?
else
@page = Page.find(params[:id])
end
end
# routes.rb
map.static 'static/:permalink', :controller => 'pages', :action => 'show'
# or
map.with_options :controller => 'info' do |info|
info.about 'about', :action => 'about'
info.contact 'contact', :action => 'contact'
info.privacy 'privacy', :action => 'privacy'
end
<%= simple_format @page.content%>
or
<%= textilize @page.content %>
or
<%= RedCloth.new(@page.content).to_html %>
本篇介绍如何在Rails应用中实现静态页面的几种方案。通过定义Pages控制器及路由规则,可以灵活地展示不同静态内容,如关于页面、联系方式等。
4万+

被折叠的 条评论
为什么被折叠?



