Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you are attempting to do is usually called "MonkeyPatch" - changing the way one module or class is working by "overriding" methods.</p> <p>It is a common practice in Rails, but it doesn't mean it is the best way to do things - when possible, it is better to use common inheritance (it is more explicit about the changes you make).</p> <p>Regarding your questions about "where to put the files": it is usually the lib/ directory. This can mean the lib of the rails app, or a lib directory inside a gem or plugin, if you are into that sort of thing.</p> <p>For example, if the file you want to change is <code>lib/generators/rails/templates/controller.rb</code> of inherited resources, the first thing you have to do is replicate that directory structure inside your lib/ folder ('lib/generators/rails/templates/controller.rb')</p> <p>Inside that new file of yours, (empty at the beginning) you can override methods. However, you must also the modules/classes hierarchy. So if the original gem had this:</p> <pre><code>module foo module bar def f1 ... end def f2 ... end end def f3 ... end end </code></pre> <p>And you wanted to modify f1, you would have to respect the foo-bar modules.</p> <pre><code>module foo module bar def f1 ... # your code here end end end </code></pre> <p>Now the last thing you need is to make sure this code is executed at the right time. If you are using the application's lib/ folder, you will need to create an entry on the <code>initializers/</code> folder and <code>require</code> your new file. If you are developing a gem/plugin, you will have a init.rb file on the "root" folder of that plugin. Put the 'require' there.</p> <p>I'm not very familiar with this <code>unloadable</code> stuff; maybe I'm asking something obvious but- have you tried making your extension module unloadable, too? (You shouldn't need this if you monkeypatched the module instead of creating a new one)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload