In this chapter, we will take a look at some common errors that occur during
Rails development and how to fix these errors
rbenv path not added
If the rbenv path is not added in the .zshrc config file then the following
error can appear:
To fix this, verify that rbenv path is added in the .zshrc using the
following command:
The above command will open the .zshrc file. In this file check for the
rbenv path. If the path is not found then add the following lines to the
config file and save it:
Ruby installation missing psych error
Ruby needs libyaml library to parse and emit YAML. If this library is not
present on the system, one may encounter a missing psych error while installing
Ruby.
To fix this error, install libyaml using Homebrew by running the following
command:
Once the installation of the library is complete, restart the terminal and
install the appropriate version of Ruby using rbenv.
You should now be able to install Ruby without any issues.
If the above steps do not solve the issue, you may need to link libyaml
manually by running the following command after the installation of the library:
Note that this step should only be performed if the previous steps have failed
to resolve the issue.
rails command does not exist
Sometimes the following error can occur on running a command like
bundle exec rails server:
This error can occur if the rails gem is installed in a different Ruby version
than the one being used globally. To fix this you should first set the desired
Ruby version as the global version and then install the rails gem in that
version.
For example, to set the global ruby version to 3.3.5, run the following
command:
Make sure that the required Ruby version is installed in the system. You can
check which versions are installed using the following command:
To install another Ruby version, you should first update the ruby-build to get
an updated list of all the available versions. Run the following command to do
so:
Now check the available versions using the following command:
Ruby and Apple Silicon
If you are using Apple silicon, then you can run into issues while installing a
Ruby version because your computer could have two different brew installations.
One for arm architecture under the /opt/homebrew directory and another one for
the Intel architecture(using Rosetta) under the usr/local directory.
To avoid such issues remove the arm version and make sure the terminal is using
Rosetta. Please refer to
this section for
steps to remove the arm brew and then refer to
enabling Rosetta on Mac section
for steps to install homebrew under Rosetta.
Fixing failing ruby-build
You can get the following error if your ruby-build is failing during an
operation:
To fix this issue, remove the command line tools and reinstall xcode using the
following command:
Then install the necessary packages:
No response from PostgreSQL service
Using an arm terminal to start the PostgreSQL service when it is installed in
the Intel compiled brew can lead to an error. You can verify the error by
running the following command:
The issue can be resolved by stopping the service from the arm terminal and
quitting the terminal and starting the service from the Rosetta terminal.
You can start or stop a service using the following commands:
Sass syntax error in production
If the assets aren't pre-compiled in production then adding tailwind imports
to your application.css file can throw the following error:
This can happen, if tailwindcss is listed as a devDependency in your
package.json file. To fix this move tailwindcss to the dependencies
section of package.json.
PostgreSQL disallowing indexing on large fields
If the field being indexed in PostgreSQL exceeds the buffer page size, it can
result in the following error being thrown:
Indexing on columns that stores large data is not a good practice since it can
be highly inefficient. Hence the simplest solution is to remove such indexes
altogether.
If it is indispensable, as a workaround, you can create a hash of the oversized
field and index the hash instead of the actual field.
Miscellaneous errors
-
Often developers miss out on restarting the server after making changes to the
Rails config files or application secrets files. These files are only loaded
once when the server is started hence it is a good practice to restart the
server after making a change to such files to avoid any errors.
-
Avoid syntax errors. For example missing out on colons or not ending a block
can lead to syntax errors. These are hard to catch. Best way to check the line
number in the stacktrace and look at the codebase.