0

I know this has been asked a lot, but I've looked at all the simple solutions and they're not working.

Firstly, .js.erb responses are working for all my other controllers...I generated a new controller and it won't respond to .js.erb

  def report
    @report = Report.all
    respond_to do |format|
      format.js do
        render :content_type => 'text/javascript'
      end
    end
  end

This route does work and will render html. I have no idea why .js.erb would work for my other controllers and not for a new one. I've done plenty of fiddling before making this new one...so debugging will be hard for me.

Charles
  • 50,943
  • 13
  • 104
  • 142
Kevin Brown
  • 12,602
  • 34
  • 95
  • 155

2 Answers2

1

Remove the entire respond_to block. Rails will pick the correct view:

def report
  @report = Report.all
end
user229044
  • 232,980
  • 40
  • 330
  • 338
  • It will pick html and not js. – Kevin Brown Jun 12 '13 at 02:48
  • Even if all I have in my as a constructor is `respond_to :js` – Kevin Brown Jun 12 '13 at 02:48
  • If I want to render html and then .js, it won't work, right? Rails will only render one or the other? I want to render html then initialize some jquery stuff from the .js. Is this bad practice? – Kevin Brown Jun 12 '13 at 12:55
  • @KevinBrown If it's choosing the HTML template, it's because your not making a request with the correct content-type. You do *not* need a `respond_to` block to choose between rendering a `.js.erb` file and a `.html.erb` file. Rails does that much automatically, **if** your request is made correctly. – user229044 Jun 12 '13 at 13:53
  • I'm sorry I'm not explaining clearly. I want to render html first AND js.erb. The .js.erb needs to have some jquery to initialize things on the page, so there are no ajax calls back to the controller. I'm trying to use `:javascript` in my .haml, but jquery isn't initialized yet so that's not working either. The weird thing is, this has all worked so far in my other controllers. I've tried rendering the .js.erb as a partial in the .haml, but it outputs as html... – Kevin Brown Jun 12 '13 at 14:03
  • Oh. Then, that's impossible. You can't render twice. You render either HTML **OR** JS in response to a request, but never both. This is unsupported by Rails, unsupported by the browser, and fundamentally unsupported by the underlying HTTP protocol. You can embed JavaScript in your HTML, but that doesn't involve a second view. – user229044 Jun 12 '13 at 14:33
  • I suppose I was just confused. In my other controllers, for some reason, I've been able to call a simple `alert('abc');` from `index.js.erb` and it works on the html render! Which is really helpful, right? I guess the best thing (I want to use chart.js, mostly) is to just use `:javascript` in the .haml.html? – Kevin Brown Jun 12 '13 at 14:35
0

If you are getting a 406

You need to explicitly disable erb's layout template checking.

render layout: false

You can keep respond_to and format if you need them for supporting other formats.

(NOTE: dynamic js for Content Security Policy purposes?)

Sudrien
  • 11
  • 1
  • the error `406` is not only raised in case of `MissingExactTemplate` but also `UnknownFormat`, and i think this question's case about `UnknownFormat`, so `layout: false` may be not help. – Lam Phan Jul 01 '21 at 04:54