Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails "Template is missing" error, though it exists (3.2.1)

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

In routes.rb I have

resources :pages

In app/controllers/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 which I want rendered when I request "/pages". However, I get the error

Template is missing

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

I've been using stackoverflow for ages without posting, but so many different things seem to trigger this error that it's hard to find answers for my particular case. Also I'm a noob :3 Please help!

like image 957
hidenori Avatar asked Feb 08 '12 20:02

hidenori


2 Answers

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

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

like image 85
Dave Isaacs Avatar answered Oct 01 '22 03:10

Dave Isaacs


It's looking to find it in app/views/pages/index but you have it in app/views/home/pages/index. That slight 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, like :pages, it will automatically look in app/views/pages.

like image 38
MrDanA Avatar answered Oct 01 '22 03:10

MrDanA