I'm coming from PHP development and I'm trying to learn Ruby on Rails. I'd like to know what is the best practice for inserting headers and footers? Typically in PHP i'd just do include('header.php');
and then include('footer.php');
Now I'm trying to learn Ruby on Rails and trying to understand how or where I should put these header/footer files?
I created a new application
rails new new_app
Then I generated a new controller
rails generate controller SignUp
This created some files and folders. I've developed some HTML inside the new new_app/views/sign_up
folder, but I'd like to include header and footer to this page and for future pages. Where should I have these files? In the same folder? or under the default folder in new_app/views/layouts
? Also, how do I even include files once I created them?
I'm new to ruby on rails development and I'd like to gain some knowledge from experts! Thanks!
In the new_app/views/layouts
there will be a file called application.html.erb
. Open that file and put your header content above where it is written <%= yield>
and footer content below <%=yield>
.
I usually make a parital in layouts file called _header.html.erb
and _footer.html.erb
and do something like this :-
<%= render "layouts/header" %> <%=yield %> <%= render "layouts/footer" %>
You could create a partial for header and footer, and include them using render
in the layout file, if you have dynamic content. Else you could just edit your layout file to have the header and footer.
Your scaffold generated views for SignUp, will all use the layout file to render the final HTML, so everything in the app/views/layouts/application.html.erb
file will get included in the output (assuming that you are using this file as the layout file for your view).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With