<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marcelo Casiraghi on Medium]]></title>
        <description><![CDATA[Stories by Marcelo Casiraghi on Medium]]></description>
        <link>https://medium.com/@cheloeloelo?source=rss-6ea2915feaab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aRgYmQg3rE_iUu2kFraRWw.jpeg</url>
            <title>Stories by Marcelo Casiraghi on Medium</title>
            <link>https://medium.com/@cheloeloelo?source=rss-6ea2915feaab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 23:02:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cheloeloelo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding Zeitwerk in Rails 6]]></title>
            <link>https://medium.com/cedarcode/understanding-zeitwerk-in-rails-6-f168a9f09a1f?source=rss-6ea2915feaab------2</link>
            <guid isPermaLink="false">https://medium.com/p/f168a9f09a1f</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails-6]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[zeitwerk]]></category>
            <dc:creator><![CDATA[Marcelo Casiraghi]]></dc:creator>
            <pubDate>Thu, 26 Sep 2019 17:44:53 GMT</pubDate>
            <atom:updated>2021-10-27T13:05:20.931Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SSL_6pgT7UBO6X5F" /></figure><p>Zeitwerk is the new code loader engine used in Rails 6. It’s meant to be the new default for all Rails 6+ projects replacing the old <em>classic</em> engine.</p><p>If we judge by its features, Zeitwerk mode <em>seems</em> to behave pretty much the same as the previous <em>classic</em> mode. The basics are very similar: models, controllers, helpers and so forth will continue to be autoloaded, reloaded and eager loaded as they have always been.</p><p>However, the main difference resides in the implementation. Zeitwerk uses a better strategy for loading things, which solves all of the <a href="https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#common-gotchas">known gotchas</a> that <em>classic</em> mode carries.</p><p>Also, something exciting is that Zeitwerk comes as a <a href="https://github.com/fxn/zeitwerk">gem</a>, which means you can leverage it inside other non-rails projects.</p><h3>Why is it better?</h3><p>Just like <em>classic</em>, Zeitwerk provides the features of code <strong>autoloading</strong>, <strong>eager loading</strong>, and <strong>reloading</strong>.</p><p>However, while <em>classic</em> mode relies on the const_missing callback for loading files, Zeitwerk uses Ruby’s native Module#autoload method.</p><p>Let’s explore the differences to understand why the latter is better.</p><h4>How <em>classic</em> mode autoloads</h4><p><em>Classic</em> mode algorithm mainly relies on two factors: autoload_paths and the use of Ruby’s <a href="https://ruby-doc.org/core-2.6.3/Module.html#method-i-const_missing">const_missing</a> callback.</p><p>How does it work?</p><p>During code execution, every time Ruby finds an unknown constant reference a const_missing callback is fired. Rails overrides the default const_missing callback from Ruby, which would normally just raise a NameError. Instead, it performs an attempt to load the file associated with the constant being looked up.</p><p>This is when autoload_paths comes into play. Rails walks through the autoload_paths list looking for the “snake case” version of the referenced constant and, if it exists, loads/requires the file.</p><p>If everything works well, the file is required, the constant gets defined, and the code execution continues.</p><p>This works fine, but it has a few limitations.</p><p>For example, there’s a <a href="https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#when-constants-aren-t-missed">well-known issue</a> that comes with <em>classic</em> mode known as <em>When constants aren’t missed</em>. Basically, it describes how you can easily introduce code that depends on the order in which files are autoloaded to work properly.</p><p>How so? Let’s see a quick example. Consider the following:</p><pre># app/models/user.rb<br>class User &lt; ApplicationRecord<br>end</pre><pre># app/models/admin/user.rb<br>module Admin<br>  class User &lt; ApplicationRecord<br>  end<br>end</pre><pre># app/models/admin/user_manager.rb<br>module Admin<br>  class UserManager<br>    def self.all<br>      User.all # Want to load all admin users<br>    end<br>  end<br>end</pre><p>In this case, if the constant Admin::User was already loaded at the time Admin::UserManager.all was called, then it would return Admin::User objects.</p><p>However, if Admin::User was not yet auto-loaded, but User was, Admin::UserManager.all would instead return User objects!</p><p>This is obviously highly undesirable, and the source of the problem comes from design: <em>classic</em> relies on theconst_missing callback and this callback is only fired as the last step of the lookup algorithm. So by construction, you can’t avoid having to deal with these issues at the application level.</p><p>So, how does Zeitwerk fix this? Continue reading…</p><h4>Zeitwerk autoloading</h4><p>Similarly to <em>classic</em> mode, Zeitwerk also relies on two factors. The first is the list of autoload_paths (they are actually called <em>root directories</em>, but they are still referenced as autoload_paths in the context of Rails).</p><p>The second factor and the main difference with <em>classic</em> is that Zeitwerk relies on Ruby’s native <a href="https://ruby-doc.org/core-2.6.3/Module.html#method-i-autoload">Module#autoload</a> method for triggering the autoloading of constants.</p><p>To understand it, let’s consider the following example. Let’s say you have a Rails 6 app with the following models:</p><pre>app/<br>  models/<br>    comment.rb<br>    post.rb</pre><p>When the project boots, Rails will call Zeitwek#setup. This method takes care of setting up the autoloaders for all of the known autoload_paths (they won’t be loaded yet though).</p><p>In the example, app/models/ directory is included in the autoload_paths, so Zeitwerk will set up a Module#autoload for each one of the constants that are inferred by the name of the files inside that directory.</p><p>In this case, Zeitwerk will infer that comment.rb and post.rb should define the constants Comment and Post respectively.</p><p>So here’s the magic; Zeitwerk will execute the following code on your behalf:</p><pre>autoload :Comment, Rails.root.join(&#39;app/models/comment.rb&#39;)<br>autoload :Post, Rails.root.join(&#39;app/models/post.rb&#39;)</pre><p>It’s as simple as that! But, what does the above do? Let’s dig into how autoload fits in this process.</p><p>Every time a Ruby program evaluates a constant there are certain steps that occur in order. For example, when Ruby evaluates the Post constant:</p><ul><li>First, Ruby checks if there’s already a stored reference for :Post in the <a href="https://en.wikipedia.org/wiki/Symbol_table">symbol table</a>. If there’s one, it returns the pointer to the class definition.</li><li>If it doesn’t find one, then Ruby checks if there’s an autoload set up for :Post</li><li>If there’s no autoload in place, then Ruby fires a const_missing callback.</li><li>However, if an autoload is indeed defined (like in our example), Ruby requires the path that was passed in as an argument to the autoload call (Rails.root.join(&#39;app/models/post.rb&#39;)) and then expects the required file to define Post</li></ul><p>And just like that, the Post constant is auto-loaded!</p><p>This works great and is a better approach than <em>classic. </em>Why? Because autoload is a built-in feature in Ruby, so instead of listening on const_missing and manually loading stuff (which can get hacky) we get to use the method that is served by the virtual machine to solve the very same purpose!</p><blockquote>Note: Zeitwerk relies on the convention that each file will define the constant that is named after the name of the file (meaning that <em>/comment.rb</em> should define the<em>Comment</em> constant). Luckily, this is not surprising for a normal Rails app.</blockquote><h3>Zeitwerk and Rails 6</h3><p>Zeitwerk comes enabled with Rails 6 by default.</p><p>If you are upgrading, load_defaults &quot;6.0&quot; will set Zeitwerk as the default autoloader for your project and you’ll have it for free.</p><p>But, if for any reason you don’t want to use it, you can always opt-out by setting config.autoloader = :classic in your application.rb.</p><h3>Gem usage</h3><p>One of the greatest advantages of Zeitwerk is that it’s built as a separate gem from Rails. So, if you are writing or maintaining a gem, you can easily add Zeitwerk as a dependency and you’ll be able to forget about require statements!</p><p>You can read the up-to-date doc <a href="https://github.com/fxn/zeitwerk#synopsis">here</a>, but mainly you just need to do:</p><pre># lib/my_gem.rb (main file)<br><br>require &quot;zeitwerk&quot;<br>loader = Zeitwerk::Loader.for_gem<br>loader.setup # ready!<br><br>module MyGem<br>  # ...<br>end<br><br>loader.eager_load # optionally</pre><p>And that’s it!</p><p>Check this sample diff from the nanoc gem when they introduced Zeitwerk to their project: <a href="https://github.com/nanoc/nanoc/pull/1403/files">https://github.com/nanoc/nanoc/pull/1403/files</a> and look at all the code they were able to delete.</p><p>To me, this approach is highly desirable because manually writing and maintaining requires is error-prone and fragile if you don’t do it carefully. With this gem, you can just forget all about it!</p><p>Thanks to <a href="https://medium.com/u/e8c8d320bc7">Xavier Noria</a> and all the contributors that put the Zeitwerk project together!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f168a9f09a1f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cedarcode/understanding-zeitwerk-in-rails-6-f168a9f09a1f">Understanding Zeitwerk in Rails 6</a> was originally published in <a href="https://medium.com/cedarcode">Cedarcode</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Installing Stimulus JS in a Rails app]]></title>
            <link>https://medium.com/cedarcode/installing-stimulus-js-in-a-rails-app-c8564ba51ea2?source=rss-6ea2915feaab------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8564ba51ea2</guid>
            <category><![CDATA[rails-5]]></category>
            <category><![CDATA[stimulus]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[stimulusjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Marcelo Casiraghi]]></dc:creator>
            <pubDate>Tue, 03 Apr 2018 19:11:32 GMT</pubDate>
            <atom:updated>2018-07-23T14:30:14.374Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CugpD67GJkeJ887gof3jlw.jpeg" /></figure><p>The purpose of this post is to explain how someone would install Stimulus JS in an existing Rails application and show some examples/recommendations on how to organize code within it.</p><p>This article is not to about explaining the Stimulus JS framework. If you are beginning with Stimulus, I’d recommend checking out their <a href="https://stimulusjs.org/handbook/introduction">getting started</a> guide, which is super lightweight and very easy to read.</p><h3>Integrating Stimulus JS with a Rails application</h3><p>Using Stimulus inside a Rails app should be fairly easy. Depending on your application stack, the installation can be a little different. It will be mainly a matter of whether your app is using webpacker or not.</p><p>If you’re not interested in using webpacker, you can skip the next section and jump right into the <a href="#6bc7"><em>Stimulus without webpacker</em></a> part.</p><h4>Stimulus with webpacker</h4><p>If your Rails app is using the <a href="https://github.com/rails/webpacker">webpacker gem</a>, installing Stimulus should be pretty straight forward — just a few commands away.</p><p>The webpacker gem provides a <a href="https://github.com/rails/webpacker#stimulus">task</a> for installing everything necessary to run Stimulus JS. Go to your Rails root directory and run the following command:</p><pre>bundle exec rails webpacker:install:stimulus</pre><p>This will add Stimulus to your package.json, just like if you had run yarn add stimulus. Additionally, it will add the following code inside app/javascripts/packs/application.js:</p><pre>// app/javascripts/packs/application.js</pre><pre>import { Application } from &quot;stimulus&quot;<br>import { definitionsFromContext } from &quot;stimulus/webpack-helpers&quot;</pre><pre>const application = Application.start()<br>const context = require.context(&quot;controllers&quot;, true, /.js$/)<br>application.load(definitionsFromContext(context))</pre><p>This is the code that will boot the Stimulus application so that it can detect the Stimulus annotations declared in your HTML views.</p><p>You also need to make sure that app/javascripts/packs/application.js is being properly loaded. If you don’t have it yet, add the following code to your layout or view:</p><pre>&lt;%= javascript_pack_tag &quot;application&quot; %&gt;</pre><p>Just like javascript_include_tag, <a href="https://github.com/rails/webpacker#usage">javascript_pack_tag</a> will load a JS file from app/javascripts/packs/, which typically acts as the entry point for a webpack application.</p><p>With that in place, you should be able to start adding some Stimulus magic into your HTML. And if you placed your Stimulus controllers under app/javascripts/controllers/, then everything should just work. Let’s see a hello_controller example.</p><p><strong>Example</strong></p><p>Add this HTML into a Rails view where your pack application is loaded:</p><pre>&lt;div data-controller=<strong>&quot;hello&quot;</strong>&gt;<br>  &lt;h1 data-target=<strong>&quot;hello.output&quot;</strong>&gt;&lt;/h1&gt;<br>&lt;/div&gt;</pre><p>Then add the following Stimulus controller under app/javascripts/controllers/hello_controller.js:</p><pre>// app/javascripts/controllers/hello_controller.js</pre><pre>import { Controller } from &quot;stimulus&quot;</pre><pre>export default class extends Controller {<br>  static <em>targets </em>= [ &quot;output&quot; ]</pre><pre>  connect() {<br>    this.outputTarget.textContent = &#39;Hello, Stimulus!&#39;<br>  }<br>}</pre><p>Reload the page. That’s all!</p><p>If you see “Hello, Stimulus!” rendered on the page, it means everything is working. You can now start adding more controllers and behavior to your application.</p><h4>Stimulus without webpacker</h4><p>Of course, you can use Stimulus JS without webpacker. Stimulus official website states:</p><blockquote>If you prefer not to use a build system, you can load Stimulus in a <em>&lt;script&gt;</em> tag and it will be globally available through the <em>window.Stimulus</em> object. <a href="https://stimulusjs.org/handbook/installing#using-without-a-build-system">[stimulusjs.org]</a></blockquote><p>If you want to do that while still taking advantage of Rails’ asset pipeline, you’ll need to download the Stimulus JS file and install it in your project manually.</p><p>Use the following command to install stimulus.umd.js under your vendor/ folder:</p><pre>wget -O vendor/assets/javascripts/stimulus.umd.js <a href="https://unpkg.com/stimulus/dist/stimulus.umd.js%60">https://unpkg.com/stimulus/dist/stimulus.umd.js</a></pre><p>(Here’s a <a href="https://stimulusjs.org/handbook/installing#using-without-a-build-system">handy link to the Stimulus website</a> where you can make sure we are still using the latest url).</p><p>You should now have a new vendor/assets/javascripts/stimulus.umd.js file in your project. This will expose a window.Stimulus object for you to use, which will be useful for starting the Stimulus application.</p><p>Now create an app/assets/javascripts/stimulus/init.js file, where you’ll require the stimulus framework, and start it up. Like this:</p><pre><em>//= require stimulus.umd</em></pre><pre>(() =&gt; {<br>  if (!(&quot;stimulus&quot; in window)) {<br>    window.stimulus = Stimulus.Application.start()<br>  }<br>})()</pre><p>We are starting the Stimulus application and attaching the resulting object to the document window. We need to do this since the intention is to share the stimulus object across the different Stimulus controllers. This will allow you to define Stimulus controllers in different JS files.</p><p>Now go ahead and require the initialization file in your application manifest, typically at app/assets/javascripts/application.js:</p><pre><em>//= require stimulus/init<br>//= require_tree .</em></pre><p>With that in place, you should be able to start creating some Stimulus controllers. Check the hello_controller example below to see it in action.</p><p><strong>Example</strong></p><p>Add this HTML into a Rails view:</p><pre>&lt;div data-controller=<strong>&quot;hello&quot;</strong>&gt;<br>  &lt;h1 data-target=<strong>&quot;hello.output&quot;</strong>&gt;&lt;/h1&gt;<br>&lt;/div&gt;</pre><p>Then the following Stimulus controller under app/assets/javascripts/controllers/hello_controller.js:</p><pre>// app/assets/javascripts/controllers/hello_controller.js</pre><pre>(() =&gt; {<br>  stimulus.register(&quot;hello&quot;, class extends Stimulus.Controller {<br>    static get targets() {<br>      return [ &quot;output&quot; ]<br>    }</pre><pre>    connect() {<br>      this.outputTarget.textContent = &#39;Hello, Stimulus!&#39;<br>    }<br>  })<br>})()</pre><p>Reload the page. It works!</p><p>Notice how the controller code from this example is different from the version using webpacker. That’s because we lost some of the syntactic sugar that comes with Babel (included in webpacker). A few notes on this:</p><ul><li>We are using the shared stimulus object from window to register the hello_controller.</li><li>We are using static get targets() method instead of static targets [...] class properties, which aren’t supported natively <a href="https://github.com/tc39/proposal-static-class-features/">yet</a>.</li></ul><p>You should be able to add as many controllers as you need under app/assets/javascripts/controllers/. Just keep in mind that you’ll need to declare every controller using the shared stimulus object. This is how someone would register a controllers/foo_controller.js:</p><pre>stimulus.register(&quot;foo&quot;, class extends Stimulus.Controller {<br>  // ...<br>}</pre><h3>Conclusion</h3><p>Adding Stimulus JS to an existing Rails application is easy. Especially if you are already using webpacker. It’s just a matter of running a rake task and you’ll be basically ready to go.</p><p>On the other hand, Rails apps not using webpacker need a little more manual setup, like the additional plumbing needed for breaking the Stimulus controllers into different files.</p><p>Either way, any of the two options work and seem to be production-ready. So, give it a try! — if you are comfortable with the Stimulus framework, of course.</p><p>Personally, I kind of like it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8564ba51ea2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cedarcode/installing-stimulus-js-in-a-rails-app-c8564ba51ea2">Installing Stimulus JS in a Rails app</a> was originally published in <a href="https://medium.com/cedarcode">Cedarcode</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails 5.2 credentials]]></title>
            <link>https://medium.com/cedarcode/rails-5-2-credentials-9b3324851336?source=rss-6ea2915feaab------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b3324851336</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[credentials]]></category>
            <category><![CDATA[rails-5]]></category>
            <category><![CDATA[secrets]]></category>
            <dc:creator><![CDATA[Marcelo Casiraghi]]></dc:creator>
            <pubDate>Mon, 26 Mar 2018 14:07:51 GMT</pubDate>
            <atom:updated>2018-07-23T14:29:46.897Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ujxWi7VOexdE4TuJ." /></figure><p>As announced in <a href="http://weblog.rubyonrails.org/2018/3/20/Rails-5-2-RC2/">Rails 5.2.0 release candidate</a>, Rails 5.2 will go out with a brand new credentials API that will eventually replace the current config/secrets.yml and config/secrets.yml.enc.</p><p>The intention behind this change is mainly to remove some of the confusion introduced by the combinations of config/secrets.yml, config/secrets.yml.enc and SECRET_BASE_KEY used in earlier versions of Rails, and attempt to unify everything using a more straightforward approach.</p><p>From now on, Rails 5.2 will only intend to use these two files instead:</p><ul><li>config/credentials.yml.enc</li><li>config/master.key</li></ul><h3><strong>Using new Rails credentials</strong></h3><p>When you upgrade Rails to v5.2, the file where you are going to store all your private credentials is config/credentials.yml.enc.</p><p>As it’s extension suggests, this file is going to be encrypted - so you won’t be able to read what’s inside of it - unless you have the proper master key to decrypt it. <strong>This is why it IS safe to version this file into your source control tool.</strong></p><p>The second file, config/master.key, is the file where your RAILS_MASTER_KEY will be placed.</p><p>The RAILS_MASTER_KEY is the key that Rails will use to decrypt your config/credentials.yml.enc. <strong>It is NOT a good idea to version </strong><strong>config/master.key file into your source control tool. </strong>You need to keep it as a secret, so make sure to list it in your <em>.gitignore</em> file if you’re using git.</p><h3><strong>Editing Credentials</strong></h3><p>Since it’s encrypted, Rails 5.2 comes with a way to edit the config/credentials.yml.enc file. You can do that by running the following command:</p><pre>EDITOR=vim rails credentials:edit</pre><p>This will open up a vim editor with the decrypted version of the file (you can use others editors too, not necessarily vim). When you save it, it will encrypt it again using your master key.</p><p>You’ll see that the decrypted version of the file looks like any other normal <em>.yml</em> file.</p><p>By the time you run the credentials:edit command, If config/master.key and config/credentials.yml.enc don’t exist, Rails will take care of creating them on your behalf.</p><h3><strong>Reading credentials</strong></h3><p>Let’s assume that the unencrypted version of your config/credentials.yml.enc file looks like this:</p><pre>aws:<br>  access_key_id: 123<br>  secret_access_key: 345<br>secret_key_base: 2fdea1259c6660852864f9726616df64c8cd</pre><p>Then, you should be able to access the configuration programmatically like this:</p><pre>Rails.application.credentials.aws[:access_key_id]     # =&gt; &quot;123&quot;<br>Rails.application.credentials.aws[:secret_access_key] # =&gt; &quot;345&quot;<br>Rails.application.credentials.secret_key_base         # =&gt; &quot;2fdea...</pre><h3>Credentials &amp; Environments</h3><p>The Rails team is proposing to remove the environment scopes inside config/credentials.yml.enc.</p><blockquote>This new file is a flat format, not divided by environments, like secrets.yml has been. Most of the time, these credentials are only relevant in production, and if someone does need to have some keys duplicated for different environments, it can be done by hand. <a href="https://github.com/rails/rails/pull/30067"><em>[DHH]</em></a></blockquote><p>Nevertheless, you can always edit config/credentials.yml.enc and add namespaces for your secrets under <em>development</em>, <em>test </em>or <em>production </em>like the ones you would have in the old config/secrets.yml. You can structure your config/credentials.yml.enc like this:</p><pre>development:<br>  aws:<br>    access_key_id: 123<br>    secret_access_key: 345<br>  secret_key_base: 2fdea1259c6660852864f9726616df64c8cd</pre><pre>production:<br>  aws:<br>    access_key_id: 321<br>    secret_access_key: 543<br>  secret_key_base: hu23ih41iu23h4123u4h23iu4h2323j412i3</pre><p>Then, simply add the environment namespace when you access the configs:</p><pre>Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]</pre><h3><strong>Deploying master key</strong></h3><p>When you move your code to a server, you need to make sure that your config/credentials.yml.enc file can be decrypted. That means that somehow you’ll need to provide Rails with your master key, given that it is not checked into version control.</p><p>There are two ways of doing that:</p><ul><li>Option 1: Place the config/master.key file in the server. You’ll normally want to symlink this file to a shared folder in the server filesystem. Again, do not version your config/master.key file.</li><li>Option 2: create a RAILS_MASTER_KEY ENV variable. Rails will detect it and use it as your master key, e.g. in heroku: heroku config:set RAILS_MASTER_KEY=&lt;your-master-key-here&gt;.</li></ul><p>You should be able to use any of those indistinctly.</p><h3><strong>Upgrading from previous Rails</strong></h3><p>Keep in mind that the config/secrets.yml and config/secrets.yml.enc will continue to work and will still be compatible with Rails 5.2</p><blockquote>Note: We will just keep Rails.secrets and friends around. The Rails.application.credentials setup will be a new, concurrent approach. All new apps would use it, but we wouldn’t need to screw existing apps. <a href="https://github.com/rails/rails/pull/30067"><em>[DHH]</em></a></blockquote><p>That won’t be forever though, so it’s not a bad idea to think about upgrading soon.</p><p>If you are upgrading your app, and not starting from a brand new Rails 5.2 app, make sure to add the following configuration to your config/environments/production.rb file:</p><pre>config.require_master_key = true</pre><p>This will tell Rails to ensure that a master key has been made available from either ENV[&#39;RAILS_MASTER_KEY&#39;] or in config/master.key.</p><h3>Conclusion</h3><p>In general, I understand what the motivation behind this change is and I support it. If the old approach was confusing for a lot of people I believe it’s a good thing that the Rails team tries to improve it. Hopefully people will adopt it and it will become the standard for everyone.</p><p>If you want to read more about this, you can take a look at <a href="https://medium.com/u/54bcbf647830">DHH</a>’s <a href="https://github.com/rails/rails/pull/30067">Pull Request</a> for the initial description and implementation of the change.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b3324851336" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cedarcode/rails-5-2-credentials-9b3324851336">Rails 5.2 credentials</a> was originally published in <a href="https://medium.com/cedarcode">Cedarcode</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>