tgandrews
tgandrews

Reputation: 12660

Rails 3 rendering template missing without file ending

I have an action in my controller as below:

 def show
    @post = Post.find_by_setitle(params[:setitle])

    if !@post
      render 'error_pages/404'
      return
    end

    respond_to do |format|
      format.html
    end
  end

If the render error_pages/404 I get a template missing. Switching it to render error_pages/404.haml.html works fine.

Why is this?

N.B. There is no actual error_pages controller or model. Just a convenient place to keep them.

Edit: I'm using mongoid and hence don't have access to ActiveRecord. Controller base can't be looking for a particular ActiveRecord exception?

Upvotes: 2

Views: 1292

Answers (2)

Zabba
Zabba

Reputation: 65467

You should probably use render :template => 'error_pages/404'. I think Rails is looking for a partial called _404.

Try it out 1:

render 'error_pages/404' (and name the file _404.html.erb)

Try it out 2:

render :template => 'error_pages/404' (and name the file 404.html.erb i.e. no leading underscore)

Upvotes: 0

Chubas
Chubas

Reputation: 18043

From the documentation

The render method can also use a view that’s entirely outside of your application (perhaps you’re sharing views between two Rails applications):

Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the :file option (which was required on Rails 2.2 and earlier):

You need either to pass the :file option, or to start the location string with a slash. Alternatively, you could use the Rails functionality to rescue from errors, and recover from ActiveRecord::RecordNotFound with a 404. See this post for details.

Upvotes: 3

Related Questions