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!

8
  • if you set a <% binding.pry %> or <% byebug %> or whatever you'r using to debug, does it stop in the template ?
    – fanta
    Oct 19, 2016 at 22:03
  • @fanta In js-template?
    – Ngoral
    Oct 19, 2016 at 22:07
  • 1
    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. Oct 19, 2016 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, 2016 at 22:55
  • 1
    You can try this in your action. format.js { render layout: false, content_type: 'text/javascript' } Oct 19, 2016 at 23:41

1 Answer 1

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.

1
  • 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, 2016 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.