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 .
9
  • What's inside your application.js? Commented Aug 2, 2015 at 14:49
  • Thanks naylaw. I have added it Commented Aug 2, 2015 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. Commented Aug 2, 2015 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? Commented Aug 2, 2015 at 15:03
  • 1
    @zkytony: Structuring js files to load per page / controller is not that trivial. brandonhilkert.com/blog/… might help. Commented Aug 2, 2015 at 15:23

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

What should i have to do if have to load a js file after a redirect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.