Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

ROR static page not loading application.html.erb

I am rather new to Ruby on Rails. I am playing with an app and added a new static page by adding a controller name pages_controller.rb, a new file views/pages/home.html.erb, and updated the routs to show the page on localhost:3000/home. It shows just fine, but it dosent load up the data from applications.html.erb that the other pages do, this file contains all the header info and the navigation and footer. Do you have an ideas on how I can get it added?

4 Answers

Ah, so your pages controller should look something like this:

#Define a generic method
def generic
  respond_to do |format|
    format.html
    end
end

#this one is not generic, i want to render a different layout
def home
  @users = User.all.count
  @blogs = Post.all.count
  @courses = Course.all
  render layout: "homepage"
end

#this is generic with view games.html.erb
def games
  generic
end

#this is generic with view contact.html.erb
def contact
  generic
end

So what I did was define the generic method, which just renders the view. Then any page that is ALL static html, then put the method (or page name) and generic inside.

Notice my home method is for the homepage. I have a different layout (not application.html.erb) that is rendered and I have some instance variables saved for use in that view. This is how you could make database calls to add data to a static (or in this case, semi-static) page.

Hope that helps!

What is in your controller? Also, what is the info you are talking about loading? CSS, DB info?

right now I dont have anything in the controller, I am just trying to load the html code (containing the nav bar and the footer) from the application.html.erb that is loaded on all the other dynamic pages that rails created. On a side note I think I may want to pull some DB info at a later point, will that become difficult?

Wow, that is just what I was looking for thank you for your help