1
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?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • 1
    Are you wanting to render a `template` or a `partial`? Partials begin with `_`. Templates do not. Also, when rendering partials, do not include the `_` in the render statement. I'm guessing you want to `render partial: 'fw_exports/import_spreadsheet'`? – jvillian Oct 30 '17 at 18:36

5 Answers5

3

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.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
2

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'

Cyzanfar
  • 6,997
  • 9
  • 43
  • 81
  • It worked for me as `render 'import_spreadsheet'`, using the `template` option gives me "Missing template /import_spreadsheet", it seems again, we're close enough - Rails 5.1.4. – Sebastián Palma Oct 30 '17 at 18:57
  • I guess one needs to provide the relative path of the template to be rendered when using `template` thanks for that! – Cyzanfar Oct 30 '17 at 18:59
  • 1
    Quick question. Since the OP is in the `import_spreadsheet` action of the `FwExportsController`, couldn't the OP just have a view named `import_spreadsheet` and skip the render line altogether? – jvillian Oct 30 '17 at 19:00
  • Yeah I think it should. Is that different for partial rendering? if not than there wouldn't be a need to explicitly render the template at the end of the action as you mention. – Cyzanfar Oct 30 '17 at 19:01
  • 1
    I *think* it's different for views than for partials. But, TBH, I don't know for a fact. I'll have to give it a go sometime. – jvillian Oct 30 '17 at 19:05
  • @Cyzanfar your answer did not work. I've already tried it, prior to asking my question. I based my code on the link you reference in your answer – EastsideDev Oct 30 '17 at 19:08
  • @EastsideDeveloper could you tell me what exactly did not work. I'm interested in seeing if there are other solutions that might work in this particular case – Cyzanfar Oct 30 '17 at 19:12
  • Hmm, it looks like I misread your answer (unless it was modified) – EastsideDev Oct 30 '17 at 19:30
1

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.

jvillian
  • 19,953
  • 5
  • 31
  • 44
0

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

Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
-2

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

Rohit Lingayat
  • 716
  • 6
  • 23
  • There is no need to rename the template. Also if other views are rendering that partial this change will break those views. – Cyzanfar Oct 30 '17 at 18:53