zkytony
zkytony

Reputation: 1498

rails 4 controller redirect not loading javascript

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 .

Upvotes: 2

Views: 1650

Answers (1)

KaizenCodes
KaizenCodes

Reputation: 131

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?

Upvotes: 2

Related Questions