Rails

Rails gem pg install failed - Lessons Learned 6

I created new Rails 7 app using Bootstrap

$rails new stockapp -c bootstrap -j esbuild

$cd stockapp/

$./bin/dev

The application worked as expected.

Then I added pg gem to Gemfile

$bundle install

However, it used to fail to install gem pg and I was getting error

Installing pg 1.4.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

 

I solved this by running following command

$sudo apt-get install libpq-dev

$bundle install

How to add bootstrap 5 to an existing Rails 7 app

I tried to install bootstrap 5 in existing Rails 7 application. The keyword "existing" is very important because for a new application you can try following.

$rails new app-name -j esbuild --css cssbundling-rails (Note: Do NOT miss -j flag to install JavaScript first. In one Youtube video, it was skipped, however it did NOT work for me for a test app I tried to install. Not sure why.)

Rails and environment variables - Lesson Learned 5

I was trying  to send emails from my Rails app and I was NOT getting any success.

As per Rails documentation, I did everything I was supposed to do.

$rails generate mailer UserMailer

Then I created method inside UserMailer class /app/mailers/user_mailer.rb and built the email templates in folder /app/views/user_mailer folder.

Then I added code to invoke the user mailer with following code.

What is the difference between render and redirect? - Lesson learned 2

-Redirect is a method that is used to issue the error message in case the page is not found or it issues a 302 to the browser. Whereas, render is a method used to create the content.

-Redirect is used to tell the browser to issue a new request. Whereas, render only works in case the controller is being set up properly with the variables that needs to be rendered.

-Redirect is used when the user needs to redirect its response to some other page or URL. Whereas, render method renders a page and generate a code of 200.

How to correct "rails generate" errors easily - Lesson learned 1

Yesterday I made a mistake and created controller with singular name instead of Plural name.

$rails generate controller Ticket

Then I realized my mistake and wanted to delete bunch of files that got created.

Easy way to delete all the files is as follows:

$rails destroy controller Ticket
$rails generate controller Tickets

It's worth mentioning the -p flag here ("p" for pretend).

Ruby on Rails - Controller

Controller

Controller takes model data from the database and then displays it on the view.

A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.

Using Rails console for debugging

Use following command from your project folder. (Note: Do not use $irb command as it will not work, although the console shows irb prompt).

$rails console # Start rails console

Use "awesome print" gem for better readability. (Refer documentation of awesome print gem.)

$require "awesome_print"

>>ap Event.all (Event is the object we want to view)

>> ObjectName.where(key1: value1).order(:key2).limit(5)