How to use an already built html template for rails
I have a开发者_如何学编程lready designed about 3 templates I wanted to make use of, but after learning rails I want to use one of them for a web application. How do i use a HTML template for a rails application?
Am asking because rails seems different due to the MVC format. So i don't know how am going to go about placing the right files in the right location.
this guide should help you get started.
Basically, you separate the main structure (header, footer) from the template and put it in the view/layout/application.html.erb
. The structure is usually similar to this:
html
head
body
#header
#content
<%= yield %>
#footer
Every controller by default uses it's own folder for view files. So views for articles
controller should be located under view/articles
folder. And every action uses it's html file.
yield
inserts the current action's view file into the layout. For example, if you go to /articles/1
it will load view/article/show.html.erb
With rails 3.0.x you put assets like stylesheets, images and JS under public
folder. 3.1 uses app/assets/
folder.
updated
Yes, you have a base layout which is the same on every page and a section that changes on every page. But of course you can use different layouts if you need to. Or you can reuse the same view file for different actions.
If you use paths relative to the root (e.g. '/stylesheets/style.css') then you don't need to change the reference for every page. Rails has some nice helpers:
stylesheet_include_tag 'stylesheet_name'
javascript_include_tag 'script_name'
You can pass several filenames, or add some options that you can find in the documentation. I get my stylesheets complied into one application.css file but if you want to keep them separate and load only when they're really needed, you can use yield
combined with content_for
. There's more info on that in the link I provided.
精彩评论