layout | title | description | permalink |
---|---|---|---|
main_guide |
Add a new homepage |
Customize your app's homepage with your own page. |
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.
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.
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 %}
{% endhighlight %}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!
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 %}
add a new link with the lines below, and save the file:
{% highlight erb %}
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?