A place for my little reminders and tips on learning Ruby on Rails
Don't wanna be here? Send us removal request.
Text
Pinterest Clone
My take on Pinterest

rails new pinterest
0 notes
Text
Add Bulma to Rails
gem bulma
bundle install
@import "bulma"; in your css
In your application.css, rename file to application.scss and add
@import "bulma"; at bottom of file
1 note
·
View note
Text
twittr clone notes
rails new twittr
rails g scaffold Tweet tweet:text
gives use views, models, controllers
rails db:migrate
Set the root route
Rails.application.routes.draw do
resources :tweets
root "tweets#index"
end
We already had esources :tweets from the scaffold earlier.
0 notes
Text
gem 'sqlite3', '~> 1.3.6'
For some reason on my ubuntu machine I need to change version of sqlite in the gem file.
Open the Gemfile inside my_first_rails_app and change gem 'sqlite3' to gem 'sqlite3', '~> 1.3.6'
https://forum.theodinproject.com/t/sqlite3-error-when-trying-to-migrate-the-database-2-3-first-rails-app/15578/10
0 notes
Text
localhost:3000/rails/info/routes
Great way to show routes and nested routes in your app. Better than rails routes
0 notes
Text
generating a resource
rails g resource registration name:string email:string how_heard:string event:references --no-test-framework
--no-test-framework ( now you won’t get the test stubs)
You get a ton of stuff.
invoke active_record create db/migrate/20190308120206_create_registrations.rb create app/models/registration.rb invoke controller create app/controllers/registrations_controller.rb invoke erb create app/views/registrations invoke helper create app/helpers/registrations_helper.rb invoke assets invoke coffee create app/assets/javascripts/registrations.coffee invoke scss create app/assets/stylesheets/registrations.scss invoke resource_route route resources :registrations
rails db:migrate
Now in registration.rb
class Registration < ApplicationRecord
belongs_to :event
end
Now we can assign an event to a registration.
0 notes
Text
gem update --system

So I was having an issue in Visual Studio Code, one of the Rails extensions was giving me error: Failed to start Solargraph
one answer suggested running
gem update --system
And I’m glad I did as it installed all kinds of enhancements and bug fixes and...
after a bit more digging around ran...
gem install solargraph
Updating rubygems-update Fetching rubygems-update-3.0.3.gem Successfully installed rubygems-update-3.0.3 Parsing documentation for rubygems-update-3.0.3 Installing ri documentation for rubygems-update-3.0.3 Installing darkfish documentation for rubygems-update-3.0.3 Done installing documentation for rubygems-update after 83 seconds Parsing documentation for rubygems-update-3.0.3 Done installing documentation for rubygems-update after 0 seconds Installing RubyGems 3.0.3 Bundler 1.17.3 installed RubyGems 3.0.3 installed Regenerating binstubs Parsing documentation for rubygems-3.0.3 Installing ri documentation for rubygems-3.0.3
=== 3.0.2 / 2019-01-01
Minor enhancements:
* Use Bundler-1.17.3. Pull request #2556 by SHIBATA Hiroshi. * Fix document flag description. Pull request #2555 by Luis Sagastume.
Bug fixes:
* Fix tests when ruby --program-suffix is used without rubygems --format-executable. Pull request #2549 by Jeremy Evans. * Fix Gem::Requirement equality comparison when ~> operator is used. Pull request #2554 by Grey Baker. * Unset SOURCE_DATE_EPOCH in the test cases. Pull request #2558 by Sorah Fukumori. * Restore SOURCE_DATE_EPOCH. Pull request #2560 by SHIBATA Hiroshi.
=== 3.0.1 / 2018-12-23
Bug fixes:
* Ensure globbed files paths are expanded. Pull request #2536 by Tony Ta. * Dup the Dir.home string before passing it on. Pull request #2545 by Charles Oliver Nutter. * Added permissions to installed files for non-owners. Pull request #2546 by SHIBATA Hiroshi. * Restore release task without hoe. Pull request #2547 by SHIBATA Hiroshi.
------------------------------------------------------------------------------
RubyGems installed the following executables: /home/dom/.rbenv/versions/2.6.1/bin/gem /home/dom/.rbenv/versions/2.6.1/bin/bundle
Ruby Interactive (ri) documentation was installed. ri is kind of like man pages for Ruby libraries. You may access it like this: ri Classname ri Classname.class_method ri Classname#instance_method If you do not wish to install this documentation in the future, use the --no-document flag, or set it as the default in your ~/.gemrc file. See 'gem help env' for details.
0 notes
Text
generate a Rails App with a postgresql database
rails new app-name -d postgresql
-d postgresql is optional and allows us to use postgresql database.
in database.yml under default: &default enter username:postgres below adapter and encoding and above pool:5
before starting the rails server run
rails db:create
0 notes
Text
Adding Bootstrap to a Rails App
Some older Rails tutorials or ones using cloud ide's running 4.2 recommend bootstrap-sass gem. "a Sass-powered version of Bootstrap 3, ready to drop right into your Sass powered applications". That was Bootstrap 3. For Bootstrap 4 use the Bootstrap ruby gem.
THE GEMS
gem 'bootstrap', '~> 4.2.1'
Bootstrap JavaScript depends on jQuery If you're using Rails 5.1+, add the jquery-rails gem to your Gemfile: gem 'jquery-rails'
bundle install and restart your server to make the files available through the pipeline.
CSS
If you have just generated a new Rails app, it may come with a .css file instead. If this file exists, it will be served instead of Sass, so rename it:
$ mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss
Then, remove all the *= require and *= require_tree statements from the Sass file. Instead, use @import to import Sass files.
The file should then contain depending on number of scss files.
They all need to be imported as we deleted require_tree which would bring them in automatically.
// Custom bootstrap variables must be set or imported *before* bootstrap.
@import "bootstrap";
@import "events";
@import "layout";
@import "show";
JAVASCRIPT
Bootstrap tooltips and popovers depend on popper.js for positioning. The bootstrap gem already depends on the popper_js gem. Add Bootstrap dependencies and Bootstrap to your application.js: above //= require tree . (pulls everything in from the js directory). //= require jquery3 //= require popper //= require bootstrap-sprockets
0 notes
Text
The new, modern clearfix hack
The new, modern clearfix hack however, is safer to use, and the following code is used for most webpages:
.clearfix::after { content: ""; clear: both; display: table; }
The Old Way
The overflow:auto clearfix works well as long as you are able to keep control of your margins and padding (else you might see scrollbars).
source. w3schools
1 note
·
View note
Text
layouts / patials
If you create a partial to render to a file in layouts folder your partial must go there too.
Same if you render to a file in the events folder like a form partial for new and edit forms the partial goes in the events folder.
0 notes
Text
migrations - rollback previously applied migration
rails db:rollback
== 20190304195318 AddImageAndCapacityToEvents: reverting ====================== -- remove_column(:events, :capacity, :integer, {:default=>1}) -> 0.0177s -- remove_column(:events, :image_file, :string, {:default=>""}) -> 0.0050s == 20190304195318 AddImageAndCapacityToEvents: reverted (0.0412s) =============
rails db:migrate:status
The status of the last migration is now down so we can go in and change the column name for example.
save then reapply the migration
== 20190304195318 AddImageAndCapacityToEvents: migrating ====================== -- add_column(:events, :image_file_name, :string, {:default=>""}) -> 0.0029s -- add_column(:events, :capacity, :integer, {:default=>1}) -> 0.0007s == 20190304195318 AddImageAndCapacityToEvents: migrated (0.0038s) =============
When you roll back the columns aren’t deleted when you run db:migrate again they will be added again minus any data that was previously entered. Rolling back is just good for editing columns etc..
0 notes
Text
migrations - create new columns
rails g migration AddImageAndCapacityToEvents image_file:string capacity:integer
Adds 2 columns to the events table
class AddImageAndCapacityToEvents < ActiveRecord::Migration[5.2]
def change
add_column :events, :image_file, :string
add_column :events, :capacity, :integer
end
end
Add the default values manually
add_column :events, :image_file, :string, default: ""
add_column :events, :capacity, :integer, default: 1
save then rails db:migrate
0 notes
Text
thinking about objects using real life objects

OBJECT: COMPUTER
Attributes
Type: Macbook
Maker: Apple
Year: 2015
Methods (functionality - a thing the object can do)
Turn On
Shut down
Create a file
0 notes
Text
Making the index page a bit more interesting (custom queries)
Early on in a rails project you might just list out all posts, tweets, events (whatever the CRUD is).
def index
@events = Event.all
end
THE WRONG WAY
allowing controller to make decisions about which events to display. Old events are gone and soonest at top of list. It’s the models job to get the data.
def index
@events = Event.where("starts_at >= ?", Time.now).order("starts_at")
end
A Better Way
Don’t put your query code in controller/views push it to methods that are defined in the model. (here event.rb)
class Event < ApplicationRecord
# if an event is free it is business logic, so goes in model
def self.upcoming
where("starts_at >= ?", Time.now).order("starts_at")
end
end
In the controller
def index
@events = Event.upcoming
end
0 notes