2

I am using Rails 4. My root_url is routed to users#new. And I want to redirect to root_url when the user logs out, which is done within the sessions controller. However, after the redirection, the javascript file which contains code in users.js is not loaded. Can anybody explain why this happens? How to resolve it?

class SessionsController < ApplicationController
    ...
    def destroy
        log_out
        redirect_to root_url
    end

If this is because users.js is specific to controller users, then what is the best solution for redirection with javascript loaded?

BTW, in my layouts/application.html.erb, I added

<%= debug(params) if Rails.env.development? %>

And on the redirected page (which is the root_url), it shows:

--- !ruby/hash:ActionController::Parameters
controller: users
action: new

I guess this means that now the controller is users. Then why isn't users.js being loaded.

I need a lesson.

---- application.js ----

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-  directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
zkytony
  • 1,242
  • 2
  • 18
  • 35
  • What's inside your `application.js`? – nayiaw Aug 02 '15 at 14:49
  • Thanks naylaw. I have added it – zkytony Aug 02 '15 at 14:56
  • By default, every js file is loaded on each page load. There is no page specific scoping. The usual suspect for Rails is Turbolinks. Remove the `//= require turbolinks` line from application.js and see whether it works. – fylooi Aug 02 '15 at 14:57
  • Hmm, can you be more specific on `users.js` is not loading? The code inside `users.js` is not functioning or the file couldn't be seen in your browser development tools under local development environment? If it's the code not functioning can you share the `users.js` as well? – nayiaw Aug 02 '15 at 15:03
  • @nayiaw I cannot see users.js being loaded using Firefox – zkytony Aug 02 '15 at 15:05
  • @fylooi Removing that line did make it work. But I think that because not all js is useful for the current page, we may not want all js to be loaded (although it is default). So is this ideal? Are there any other solution? – zkytony Aug 02 '15 at 15:07
  • @naylaw Very strange. Maybe I overlooked something. File is loaded but not executed. (Yet I believe that I saw the file not loaded before....) – zkytony Aug 02 '15 at 15:10
  • This question is in essence the same as [This Question][1] [1]: http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links – zkytony Aug 02 '15 at 15:20
  • 1
    @zkytony: Structuring js files to load per page / controller is not that trivial. http://brandonhilkert.com/blog/organizing-javascript-in-rails-application-with-turbolinks/ might help. – fylooi Aug 02 '15 at 15:23

1 Answers1

2

Turbolinks can cause problems. Inside your users.js put the code into this block:

$(document).on('ready page:load', function() {
  // your code here
});

However if your js file isn`t loaded at all the above solution might not work. If you inspect the page in the browser do you see, under resources, your js?

KaizenCodes
  • 131
  • 8