在Rails中创建静态页面的最佳方法是使用Rails的内置功能,如下所示:
StaticPagesController
。rails generate controller StaticPages
StaticPagesController
中添加要显示的静态页面的方法,例如home
和about
。class StaticPagesController< ApplicationController
def home
end
def about
end
end
app/views/static_pages/home.html.erb
和app/views/static_pages/about.html.erb
。config/routes.rb
中添加路由,将静态页面URL映射到相应的控制器方法。Rails.application.routes.draw do
get 'static_pages/home'
get 'static_pages/about'
end
http://localhost:3000/static_pages/home
和http://localhost:3000/static_pages/about
即可查看静态页面。这种方法可以让你轻松地创建和管理静态页面,同时也可以利用Rails的布局和部分模板功能,让页面更加美观和易于维护。
领取专属 10元无门槛券
手把手带您无忧上云