Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FrozenError - can't modify frozen String (ruby 2.5+)

I'm trying to update ruby in our project to 2.5 and up, and bundler from 1.17.2 to 2.0.1. After doing so, running our application produces the following error in multiple places:

FrozenError - can't modify frozen String:

This is a result of trying to set the timezone in a controller (Time.zone = current_user.location.time_zone) among other places.

like image 635
Alon Dahari Avatar asked Apr 23 '19 16:04

Alon Dahari


1 Answers

You can use Time.use_zone(zone) with a block, for example:

class ApplicationController < ActionController::Base
  around_action :set_time_zone

  def set_time_zone
    if logged_in?
      Time.use_zone(current_user.location.time_zone) { yield }
    else
      yield
    end
  end
end

https://api.rubyonrails.org/classes/Time.html#method-c-zone-3D

like image 70
Sergey Burtsev Avatar answered Sep 21 '22 08:09

Sergey Burtsev