Skip to content

Latest commit

 

History

History
80 lines (58 loc) · 2.62 KB

new-homepage.md

File metadata and controls

80 lines (58 loc) · 2.62 KB
layout title description permalink
main_guide
Add a new homepage
Customize your app's homepage with your own page.
new-homepage

Add a new homepage

{% include main-guide-intro.html %}

In this guide we'll add another page. This will be our new homepage: the first page that will be shown when you open your app when you visit http://localhost:3000. Feel free to skip this guide if you know how Rails controllers, views and routes work.

In the previous guide a "pages" controller was already generated, we do not need to do this again. Rails will stop us if we do try to. Instead, we'll need to add the page manually ourselves.

Add a new view

Instead we'll add another page on our own. Run the following command in the Terminal app to add another "view" file used to display page content.

{% highlight sh %} touch app/views/pages/homepage.html.erb {% endhighlight %}
{% highlight sh %} ni app/views/pages/homepage.html.erb {% endhighlight %}

Then open the newly created file in your Text editor: app/views/pages/homepage.html.erb

Add some content to it, like the following, and save the file:

{% highlight erb %}

The ideas app

Welcome to my ideas app!

{% endhighlight %}

Configuring the route

To tell Rails when to show this page, open the config/routes.rb file in your Text Editor. Change the following line:

{% highlight ruby %} root to: redirect("/ideas") {% endhighlight %}

to this instead and save the file:

{% highlight ruby %} root "pages#homepage" {% endhighlight %}

When you now visit the root path of the app, http://localhost:3000, you should see your new homepage!

Updating the navigation bar

Lastly, to make the new root page accessible through the navigation bar, open the app/views/layouts/application.html.erb file in your Text Editor. Above the following lines:

{% highlight erb %}

  • Ideas
  • {% endhighlight %}

    add a new link with the lines below, and save the file:

    {% highlight erb %}

  • Home
  • {% endhighlight %}

    When you refresh the page in the browser, and click the "The ideas app" link, it will open the new homepage. Try out all the links in the navigation bar. Do they take you to the page you expected?