2

I'm trying to implemet a complex task, and faced with a problem, that my JS code do not work when called from format.js.

I have a link with such code: = link_to new_user_support_letter_path, {:id => 'contact-us', :remote => true }

In my user_support_letters_controller.rb:

  def new
    @letter = UserSupportLetter.new
    respond_to do |format|
      format.js {}
      format.html
    end
  end

In new.js.erb:

alert("rrr");

So, when I click a link nothing happens.

Meaningful logs for clicking a link:

I, [2016-10-19T21:47:41.130608 #1]  INFO -- : Started GET "/user_support_letters/new" for 172.18.0.6 at 2016-10-19 21:47:41 +0000
I, [2016-10-19T21:47:41.150411 #1]  INFO -- : Processing by UserSupportLettersController#new as JS
I, [2016-10-19T21:47:41.269853 #1]  INFO -- :   Rendered user_support_letters/new.js.erb within layouts/application (0.1ms)
I, [2016-10-19T21:47:41.344629 #1]  INFO -- :   Rendered application/_support_email.slim (0.1ms)
...

Can anybody tell what I'm doing wrong? Thanx!

Ngoral
  • 4,215
  • 2
  • 20
  • 37
  • if you set a `<% binding.pry %>` or `<% byebug %>` or whatever you'r using to debug, does it stop in the template ? – fanta Oct 19 '16 at 22:03
  • @fanta In js-template? – Ngoral Oct 19 '16 at 22:07
  • 1
    https://blog.codeship.com/unobtrusive-javascript-via-ajax-rails/ check out this tutorial. Id copy the first alert example they use to trouble shoot. If their format works youll narrow down your issue. You could also change your alert in your new.js.erb to console.log("call made") then check the console to make sure the request was made and the js was executed. – user3366016 Oct 19 '16 at 22:19
  • @user3366016 Putting the code after `render js:` made it work. But moving it out to the `new.js.erb` breaks it again. Logs write like they rendering it (`Rendered user_support_letters/new.js.erb within layouts/application (1.0ms)`), but nothing happens. `console.log` makes no output. What that could be? – Ngoral Oct 19 '16 at 22:55
  • Seems like the request is working but the js isnt executed. Are you using haml templates or html? Haml needs something (forget what, just google it) in the format.js {} brackets. – user3366016 Oct 19 '16 at 23:00
  • @user3366016 How do haml/html templates are connected with js? And actually I use `slim` templates, if I.. understand technologies right :) – Ngoral Oct 19 '16 at 23:06
  • 1
    You can try this in your action. format.js { render layout: false, content_type: 'text/javascript' } – user3366016 Oct 19 '16 at 23:41
  • @user3366016 YAAAAAAAAAAAAAAAAY! That done it! Oh, god! Three days for that. Can you, please, explain why this magic is really needed? – Ngoral Oct 19 '16 at 23:45

1 Answers1

3

To get this to work you need to set layout to false and declare the content_type you're using.

From what I know/have read this is standard behaviour in rails. Here is a similar post discussing it with a link to a bug log. https://stackoverflow.com/a/1170121/3366016

Change your controller action to this:

  def new
    @letter = UserSupportLetter.new
    respond_to do |format|
      format.js { render layout: false, content_type: 'text/javascript' }
      format.html
    end
  end

By default it will try to load in the current layout for the given content type. For JS it typically is smart enough to know not to load a layout. By setting false you are disabling the layout and just telling it to render without a layout.

It seems if you are using some form of templateing it messes with the requests a bit. This answer seems to identify in the comments a specific work around for slim template. This answer describes another solution, basically using the default layout setup. With this, Rails seems to know how to handle it for JS requests.

Community
  • 1
  • 1
user3366016
  • 1,267
  • 2
  • 18
  • 31
  • Great, thank you! I've already googled several auestins with the same problems but they had no answers. /me Go and learn about layouts. – Ngoral Oct 20 '16 at 00:45