Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to use with Rails 6 and Webpacker? #45

Open
coezbek opened this issue Mar 23, 2021 · 2 comments
Open

Question: How to use with Rails 6 and Webpacker? #45

coezbek opened this issue Mar 23, 2021 · 2 comments

Comments

@coezbek
Copy link

coezbek commented Mar 23, 2021

When using Rails 6 with Webpacker for application.js, my attempts to use require the javascript dependencies have failed. Do you think you will unbundle the Javascript and support adding the JS dependencies via yarn?

@pooriajr
Copy link

pooriajr commented Apr 25, 2021

I got around this adding sprockets back alongside my existing Webpacker setup.

  1. Add //= link_directory ../javascripts .js to app/assets/config/manifest.js
  2. Create app/assets/javascripts/application.js and add the two //=require lines for this gem
  3. Add javascript_include_tag alongside javascript_path_tag in <head>

@duleorlovic
Copy link

Sorry for not advertise this gem, but you can achieve this functionality by adding a package

yarn add js-cookie

and create javascript file

// app/javascript/browser_timezone_cookie.js
import Cookies from 'js-cookie'

// https://github.com/kbaum/browser-timezone-rails/blob/master/app/assets/javascripts/browser_timezone_rails/set_time_zone.js.erb
Cookies.set(
 "browser.timezone",
  Intl.DateTimeFormat().resolvedOptions().timeZone,
  {
    expires: 365,
    path: '/'
  }
);

include it in main application pack file

// app/javascript/packs/application.js
import 'browser_timezone_cookie'

and use the cookie in around action in application controller

# app/controllers/application_controller.rb
  prepend_around_action :use_time_zone
  def use_time_zone(&action)
    Time.use_zone(Time.find_zone(cookies['browser.timezone'].presence) || Time.zone, &action)
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants