Rails "Template missing" error, although it exists (3.2.1)

I just started using Rails and not sure what I'm doing wrong.

In routes.rb I have

resources :pages 

In app / controller / pages_controller.rb I have

 class PagesController < ApplicationController def index end end 

I have a layout in app / views / layouts / application.html.erb and a template in app / views / home / pages / index.html.erb that I want to display when I request "/ pages". However, I get an error

Missing template

There are no template pages / index, application / index with {: locale => [: en] ,: formats => [: html] ,: handlers => [: erb ,: builder ,: coffee]}. Search in: * "/ ### / app / views"

I have been using stackoverflow for centuries without publishing, but so many different things seem to cause this error, that it is hard to find the answers for my particular case. Also I noob: 3 Please help!

+6
source share
3 answers

You say that you have app/views/home/pages/index.html.erb to represent the index representation of the resource of your pages. I think a home/ directory is not required.

In other words, your view file should be app/views/pages/index.html.erb .

+9
source

It searches for it in app/views/pages/index , but you have it in app/views/home/pages/index . This small difference makes it so that the Rails convention is lost.

If you must keep your new directory hierarchy, do this on your controller:

 class PagesController < ApplicationController def index render :partial => "home/pages/index" end end 

But by default, if you have a resource, for example :pages , it will automatically look in app/views/pages .

+4
source

I had this problem, and I solved it simply by changing the folder name from car to cars. I had to change the folder name from singular to plural.

0
source

Source: https://habr.com/ru/post/907984/


All Articles