user10358924
user10358924

Reputation:

No route matches [GET] "/static_pages/home"

I'm a beginner. I'm in trouble because I can't settle an error.

error message

No route matches [GET] "/static_pages/home"

Rails.root: /home/ec2-user/environment/sample_app

Application Trace | Framework Trace | Full Trace

Helper    HTTP Verb    Path    Controller#Action 
root_path    GET    /    static_pages#home
help_path    GET    /help(.:format)    static_pages#help
about_path    GET    /about(.:format)    static_pages#about
contact_path    GET    /contact(.:format)    static_pages#contact

sample_app/config/routes.rb

Rails.application.routes.draw do
 root 'static_pages#home'
 get  '/help',    to: 'static_pages#help'
 get  '/about',   to: 'static_pages#about'
 get  '/contact', to: 'static_pages#contact'
end

layout/application.html.erb

<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag    'application', media: 'all',
'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application',
'data-turbolinks-track': 'reload' %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>

layout/home.html.erb

<div class="center jumbotron">
<h1>Welcome to the Sample App</h1>

<h2>
This is the home page for the
<a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
sample application.
</h2>

<%= link_to "Sign up now!", '#', class: "btn btn-lg btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails logo"),
'http://rubyonrails.org/' %>

Upvotes: 0

Views: 428

Answers (1)

Rockwell Rice
Rockwell Rice

Reputation: 3202

It looks like, from your post, that your home.html.erb template file is in the wrong place. There should be a folder called static_pages inside app/views. So inside app/views/static_pages/ is where your home.html.erb file should be. Your code says it is inside the layouts folder. Move it and it will work.

So, specifically, either create the folder static_pages (if it does not exist) or move you home.html.erb file into the app/views/static_pages/ folder.

Just and FYI, if your other views (contact, about, etc) are in the layout folder move them into static_pages too. But leave the layout file in there.

Since you are a beginner I'll explain this a little more. When you set your routes you are telling rails where to find the files. So when you write something like get '/help', to: 'static_pages#help what you telling rails is when the URL /help is viewed serve up the file help.html.erb in the static_pages folder and it knows to look inside the app/views folder by configuration. If you wanted to keep home.html.erb where it is, you could just change you routes to root 'layouts#home' and it should work, but since the view file is not a layout file that would not be the best solution. Hope this helps.

Upvotes: 1

Related Questions