EastsideDev
EastsideDev

Reputation: 6659

Template is not found by Rails

Rails 5.1

In my controllers/fw_exports_conttroller.rb, I have:

def import_spreadsheet
  @import_spreadsheet = FwExport.new
  render template: "fw_exports/_import_spreadsheet"
end

When I select the menu item for this controller action, I get the following error message:

Processing by FwExportsController#import_spreadsheet as HTML
ActionView::MissingTemplate (Missing template fw_exports/_import_spreadsheet with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :slim, :coffee, :jbuilder]}. Searched in:
  * "/home/utils/rails/follower_wonk/app/views"
  * "/home/utils/.rvm/gems/ruby-2.4.2/gems/devise_invitable-1.7.2/app/views"
  * "/home/utils/.rvm/gems/ruby-2.4.2/gems/devise-4.3.0/app/views"
):
FATAL -- : [d37603eb-3da3-4ad2-b81b-29198e6bb773] app/controllers/fw_exports_controller.rb:67:in `import_spreadsheet'

I verified that

app/views/fw_exports/_import_spreadsheet.html.slim

does exist. Any ideas?

Upvotes: 1

Views: 1685

Answers (5)

Aaron Gray
Aaron Gray

Reputation: 11713

If you just updated to Rails 5.1 and are seeing this error, it can also be caused by using render nothing in a controller since this method was removed in Rails 5.1. This is not always immediately apparent because sometimes the controller line can be a few levels deep in your stack trace. But if you check the line numbers of all the controllers in your stack trace and you spot a render nothing, you've found the culprit. If this is indeed your issue, all you need to do is replace this old method.

For example, if your old code was: render nothing: true, status: 403

You could fix the error by changing it to: head :forbidden

Upvotes: 0

jvillian
jvillian

Reputation: 20253

Or, possibly:

class FwExportsController < ApplicationController

  def import_spreadsheet
    @import_spreadsheet = FwExport.new 
  end

end

If you have app/views/fw_exports/import_spreadsheet.html.slim (a view, not a partial), then I think it will render by default/convention.

Upvotes: 1

Cyzanfar
Cyzanfar

Reputation: 7146

From the Layout documentation and from this post it looks like you should not put the underscore of your partial.

The doc states that:

Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the :template option (which was required on Rails 2.2 and earlier)

Which means that you do not need to provide the path to the controller since you are dealing with the same one here:

As per @SebastiánPalma suggested edit

def import_spreadsheet
  @import_spreadsheet = FwExport.new
  render "fw_exports/import_spreadsheet"
end

Should simply solve your problem. Also template was required prior to Rails V2.2 but since you are using 5.1 you are not required to use it. You could simply do render 'import_spreadsheet'

Upvotes: 2

Rohit Lingayat
Rohit Lingayat

Reputation: 716

I am assuming that you need to render the Template not the Partial.

To render the Template you need to rename the _import_spreadsheet.html.slim to import_spreadsheet.html.slim.

The below modification you required in your code

Step1

def import_spreadsheet
  @import_spreadsheet = FwExport.new
  render template: "fw_exports/import_spreadsheet"
end

here you need to remove the _ from _import_spreadsheet

Step 2

Rename the file to app/views/fw_exports/import_spreadsheet.html.slim instead of app/views/fw_exports/_import_spreadsheet.html.slim

Upvotes: -2

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33471

By the "_" fw_exports/_import_spreadsheet.html.slim looks like a partial, when rendering partials you can skip the partial option and just pass the route of the file after the views folder, in your case:

def import_spreadsheet
  @import_spreadsheet = FwExport.new
  render 'fw_exports/import_spreadsheet'
end

Note render 'fw_exports/import_spreadsheet' will work even if the file doesn't have the "_" prefix, unlike the template option, that doesn't accept partial files.

Upvotes: 3

Related Questions