Rails
Reference repo - https://github.com/munderseth/my.rails
To create an example app:
rails new .
Options:
-T- no tests-d mysql- use MySQL
To force a reinstall of all gems:
bundle install --force
Development
Terms
- CRUD - Create, Read, Update, Destore
- MVC - Model (db), View (pages), Controller (logic)
- erb - Embedded Ruby (template code within html)
- Capybara (ca.pa.bar.a) -
MVC
The model-view-controller (MVC) pattern

CRUD
Web applications involve CRUD - Create, Read, Update, and Delete - operations. Rails uses the term resource to represent a collection of routes, along with controller actions and views that perform CRUD on a database model (i.e. data).
A method called resource that creates conventional routes and helper methods.
Example for an Article model:
Prefix Verb URI Pattern Controller#Action
root GET / articles#index
articles GET /articles(.:format) articles#index
new_article GET /articles/new(.:format) articles#new
article GET /articles/:id(.:format) articles#show
POST /articles(.:format) articles#create
edit_article GET /articles/:id/edit(.:format) articles#edit
PATCH /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
In addition, article_path and article_url are created.
Notes:
- Validations are rules that are checked before a
modelobject is saved.
Database
Drop:
bundle exec rake db:drop
Create:
bundle exec rake db:create db:migrate
Commands
rails consolerails routes --expanded- list all of the routes (refer here)rails generate rspec:install- setup rspec within projectirb- ruby interpretergem which <name>- shows gem locationwhereis <bin>- linux command to show location- Add to log file -
logger.debug "This is a debug message"
Debugging
logger.debug "DEBUG: #{__method__}, #{__FILE__}:#{__LINE__}"
Testing
Setup
- Update
Gemfilegem "rspec-rails"
gem "capybara"
gem "selenium-webdriver"
gem "factory_bot_rails"
gem "database_cleaner" - Bundle install
- Create
spec/support/factory_bot.rbRSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end - Uncomment "Dir[Rails.root.join].." in
spec/rails_helper.rb
References
- https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md
- https://semaphoreci.com/community/tutorials/working-effectively-with-data-factories-using-factorygirl
- https://thoughtbot.com/blog/automatically-wait-for-ajax-with-capybara
- https://github.com/teamcapybara/capybara#asynchronous-javascript-ajax-and-friends
Codespaces
There are some specific to work in Codespaces.
Requires to install rails:
gem install rails
In the controllers required to add the following before a POST (Create):
skip_before_action :verify_authenticity_token
Otherwise will not write to the database. Refer to stackoverflow question for more details.