-
Notifications
You must be signed in to change notification settings - Fork 0
Home
To start the server using procfile, the following command was used:
bundle exec rails s -p 3000This site was created with the command
rails new StudErn --database=mysql --skip-docker --skip-test --skip-bundleAfter downloading the repository, the following command was used to install the dependencies:
bundle installTo set the local gem setup on vendor directory, the following command was used:
bundle config set --local path 'vendor/bundle'Then The site was deployed to a shared hosting server using cPanel. The following steps were required to deploy the site:
-
It is required to have updated bundler. So run
gem install bundler railsto install the latest version of bundler and rails. -
It was required to have
gem 'psych', '~>4.0.0'in the Gemfile to avoid the errorCould not find 'psych' (>= 0) -
Also ruby-lsapi gem was required to install in production machine to remove the error
Could not find 'lsapi' (>= 0) -
Finally, the command
rails assets:precompilewas used to precompile the assets in production environment.
Then basic layouts and CSS were added to the site.
Now the site is ready to have more updates and features.
The site uses the gem devise for authentication. The following command was used to install the gem:
rails generate devise:installThen the following command was used to generate the User model:
rails generate devise UserDepending on your application's configuration some manual setup may be required:
-
Ensure you have defined default url options in your environments files. Here is an example of default_url_options appropriate for a development environment in config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In production, :host should be set to the actual host of your application.
Required for all applications. -
Ensure you have defined root_url to something in your config/routes.rb. For example:
root to: "home#index"
-
Ensure you have flash messages in app/views/layouts/application.html.erb. For example:
<p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p>
Not required for API-only Applications
-
You can copy Devise views (for customization) to your app by running:
rails g devise:views users
Not required
To setup multiple authentication models, we have to update the config of devise in config/initializers/devise.rb file.
config.scoped_views = trueThen we can generate the model with the following command:
rails generate devise control_unitTo customize the views of the devise model, we can generate the views with the following command:
rails generate devise:views control_unitForce to use the views of the devise model, we can use the following command:
rails generate devise:controllers control_unitUpdate the routes in config/routes.rb file:
devise_for :control_units,
path: 'control_unit',
path_names:
{
sign_in: 'login',
sign_out: 'logout',
sign_up: 'register',
confirmation: 'verification',
registration: 'account',
cancel: 'close'
},
controllers:
{
sessions: 'control_unit/sessions',
registrations: 'control_unit/registrations',
confirmations: 'control_unit/confirmations',
unlocks: 'control_unit/unlocks',
omniauth: 'control_unit/omniauth_callbacks'
}To add custom fields to the devise model, we have to generate the migration with the following command:
rails generate migration AddFieldsToControlUnit name:stringThen we have to update the migration file with the following code:
class AddFieldsToUsers < ActiveRecord::Migration[6.1]
def change
add_column :control_units, :name, :string
end
endThen we have to run the migration with the following command:
rails db:migrateTo permit the custom fields in the devise model, we have to update the application_controller.rb file with the following code:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
endor for any specific devise model, we can update the controller with the following code:
class ControlUnit::RegistrationsController < Devise::RegistrationsController
before_action :configure_permitted_parameters
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
endFor creating a new model, the following command was used: (Example: Post model)
rails g model post imageLink:string imageAlt:string title:string description:text postHealth:integer postStatus:integer user:referencesThen the migration was run with the following command:
rails db:migrateFor creating a new controller, the following command was used: (Example: Post controller)
rails g controller posts index show new create edit update destroyFor creating a new view, the following command was used: (Example: Post view)
rails g view posts index show new editFor creating a new model with scaffold, the following command was used: (Example: Post model)
rails g scaffold post imageLink:string imageAlt:string title:string description:text postHealth:integer postStatus:integer user:referencesThen the migration was run with the following command:
rails db:migrateFor creating a new layout, we need to make a new file in app/views/layouts directory.
Ex: we want to make a layout named authentications, then we have to make a file in app/views/layouts/authentications.html.erb directory.
To use the new layout, we have to update the controller with the following code:
class SomeController < ApplicationController
layout 'authentications'
endOr we can specify only one action to use the layout with the following code:
class SomeController < ApplicationController
layout 'authentications', only: [:new, :create]
endOr we can define a controller function to use the layout with the following code:
class SomeController < ApplicationController
def new
render layout: 'authentications'
end
endTo install Tailwind CSS, the following command was used:
bundle add tailwindcss-ruby
bundle add tailwindcss-rails
rails tailwindcss:installTo add Daisy UI, the following Node command can be used:
npm init -y
npm install daisyui@latestThen the following code was added to the app/assets/tailwind/application.css file:
@import "tailwindcss" source(none);
@source "../../../public/*.html";
@source "../../../app/helpers/**/*.rb";
@source "../../../app/javascript/**/*.js";
@source "../../../app/views/**/*";
@plugin "daisyui";To add Daisy UI, the following Ruby command can be used:
curl -sLo app/assets/tailwind/daisyui.js https://github.com/saadeghi/daisyui/releases/latest/download/daisyui.jsThen the following code was added to the app/assets/tailwind/application.css file:
@import "tailwindcss" source(none);
@source "../../../public/*.html";
@source "../../../app/helpers/**/*.rb";
@source "../../../app/javascript/**/*.js";
@source "../../../app/views/**/*";
@plugin "./daisyui.js";To install Sidekiq, the following command was used:
bundle add sidekiqTo setup Sidekiq, add active job adapter in the config/application.rb file:
config.active_job.queue_adapter = :sidekiqCreate a new file config/initializers/sidekiq.rb with the following code:
redis_url = "redis://#{ Rails.application.credentials.redis[:username] }:#{ Rails.application.credentials.redis[:password] }@#{ Rails.application.credentials.redis[:host] }:#{ Rails.application.credentials.redis[:port] }/0" || ENV['REDIS_URL']
redis_timeout= 30
Sidekiq.configure_server do |config|
config.redis = { url: redis_url }
end
Sidekiq.configure_client do |config|
config.redis = { url: redis_url }
endIn Production, ensure to add the REDIS_URL environment variables in the server.
OR We can add the data in the config/credentials.yml.enc file.
redis:
host: localhost
port: 6379
username: username
password: passwordNow add a new file config/sidekiq.yml with the following code:
:concurrency: 5
:queues:
- default
- mailers
- some_queue_name
:logfile: log/sidekiq.log
:pidfile: tmp/pids/sidekiq.pidWe have to create a new job with the following command:
rails generate job some_workerThen we have to update the job file with the following code:
class SomeWorker < ApplicationJob
queue_as :some_queue_name
def perform(*args)
# Do something later
end
endThen we have to call the job in the controller with the following code:
SomeWorker.perform_later
# OR
SomeWorker.set(wait: 5.minutes).perform_later
# OR
SomeWorker.perform_in(5.minutes)To start Sidekiq, the following command was used:
bundle exec sidekiq -C config/sidekiq.ymlOr add the following code to the Procfile:
web: bundle exec rails server
worker: bundle exec sidekiq -C config/sidekiq.ymlTo monitor Sidekiq, we can add the following routes to the config/routes.rb file:
# sidekiq web UI
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'To setup Internationalization, we have to add the following code to the config/application.rb file:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.available_locales = [:en, :bn]
config.i18n.default_locale = :enThen we have to add the following code to the config/locales/en.yml file:
en:
hello: "Hello world"Same way we have to add the following code to the config/locales/bn.yml file:
bn:
hello: "হ্যালো ওয়ার্ল্ড"Add the following code to the application controller:
class ApplicationController < ActionController::Base
around_action :switch_locale
def switch_locale(&action)
locale = params[:lang] || session[:lang] || I18n.default_locale
session[:lang] = locale
I18n.with_locale(locale, &action)
end
# rest of the code
endThen we can use the following code in the views:
<%= t 'hello' %>Now we can use the following URL to change the language:
For English: http://localhost:3000/?lang=en
For Bangla: http://localhost:3000/?lang=bn
This will change the language of the site and store the language in the session. More language can be added in this process.