#117 Semi-Static Pages

本篇介绍如何在Rails应用中实现静态页面的几种方案。通过定义Pages控制器及路由规则,可以灵活地展示不同静态内容,如关于页面、联系方式等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值