开发者

Ruby on Rails best practices -- create a Pages controller for shared layouts?

What's the standard way to structure a Rails app's semi-static web pages?

I come from a LAMP background and traditionally I'd have pages such as this:

about.php
index.php
common/
    header.php
    footer.php
    scripts.php
    (开发者_StackOverflow中文版etc)
sign-up.php
scripts/
    jQuery.js
    etc
styles/
    main.css
(etc)

Inside those files, there would be a php include for the header, footer, etc.

Should I generate a controller called Pages?


To answer your question straight: don't create a Controller for shared layout ... But follow the advice below:

1.about.php, index.php, sign-up.php:

about and index actions could be gathered in a controller, generally I name it Static.

So logically, views would be in /app/views/static/

For sign_up it would depend on your choice: whether or not you want it to stick to your User model. Generally, it goes to some Registration controller.

2.common/ header.php, footer.php, scripts.phpwould become:

layouts/_header.html.erb, _footer.html.erb, _scripts.html.erb + you should create a layout including these partials.

3.scripts/jQuery.js and styles/main.css will go to /public/javascriptsand /public/stylesheets (at least for Rails 3.0.x)


What you're looking for are Layouts. Essentially, you define a layout in /app/views/layouts (the default being application.html.erb), which is a "wrapper" for all of your page content. This layout can include any other files you might want, and can be dynamically modified by each individual view. You can read the official tutorial here.


This R. Bates' railscast demonstrate all what you need!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜