Ruby on Rails Explorer

Your Learning Story

Only editable by explorers

  • Last updated August 29, 2014 at 11:48 AM by hankish
  • Evidence visible to public
Share the story of how you learned Ruby on Rails. Be detailed and include links to specific resources.

All posted evidence

How I learned Rails - Part 1 - Rails Tutorial

This is my #learning-story.

I started with the The Ruby on Rails Tutorial by Michael Hartl (http://ruby.railstutorial.org/ruby-on-rails-tutorial-book).  This tutorial in general is fantastic, really really awesome.  My favorite part is how complete it is.  Michael walks you through every little detail of setting up your environment and links to lots of other useful resources.

As I went through the tutorial, though, I ended up pulling up a lot of other resources.  Below is an adapted version of my notes as I worked through the tutorial.  It took me about two months to work through the tutorial.  I was only working on it intermittently but it still took longer than I was expecting.

What the heck is REST?

The Rails Framework is deeply opinionated about being RESTful.  This was embarrassing because I thought I knew what that meant but had to do some research before I really grokked it.

The key thing about REST is that the URI is the noun and the standard HTTP methods are the verbs. (" If your URLs have action words in them, you're doing it wrong.") ... http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or-http
Walked through this tutorial on the underlying Ruby language: http://tryruby.org 

Using git

Git reference book : http://git-scm.com/book ;

First time setup of repository
$ git init
[tailor .gitignore file]
$ git add .
$ git status >> optional: checks the status
$ git commit -m "Initial commit"

Setting up remote stuff
General git reference: http://gitref.org/remotes/ 
$ git remote -v >> list configured remotes
$ git remote add origin https://github.com/hankish/repository.git >> sets up the remote (origin can be anything, repository should be the name of the configured repository on github)
$ git push -u origin master >> after one call to this you can just call "git push"
$ git remote rm origin >> removes remote
$ git remote rename origin newOrigin >> rename remote

Branching
$ git checkout -b new-branch-name >> creates a new branch (leaving out -b just switches branches)
$ git branch >> shows all branches with a star next to the active one[make your changes]
$ git status >> checks the status
$ git commit -a -m "message" >> commits all changes to EXISTING files 
or... $ git add . >> to add files, then >> $ git commit -a -m "message" >> to commit
$ git co master >> checkout master
$ git merge new-branch-name >> merges the new branch into master
$ git branch -d new-branch-name >> gets rid of the new branch (use capital -D to remove without merging first)>> to move a file without it counting as a deletion use "git mv file.rb newfile.rb"

Deploying to heroku
[first make sure you've added postgresql gem >> gem 'pg']
$ heroku login
$ heroku create "hrt-testapp" >> creates a subdomain for "http://hrt-testapp.herokuapp.com" (you can leave the name off to get an auto generated one), also adds "heroku" as a remote to git
$ git push heroku master >> pushes the master branch to the active app
$ heroku open >> opens the active app in a web browser
$ heroku rename railstutorial >> optional: rename your app
$ heroku logs >> if you have problems this opens the deployment logs

Restoring a project from github (When switching development machines)
$ git fetch origin >> still not sure what this does but it's important
$ git fetch origin master
$ git co -b cur-github origin/master >> checks out the master branch from github and saves it in a branch called "cur-github"
$ git co master >> switch back to master
$ git merge cur-github >> merges into master
$ git branch -d cur-github >> removes the github branch
$ bundle install --without production >> installs any missing gems

Other miscellaneous links


hankish Over 9 years ago

How I learned Rails - Part 4 - Building Badge List

At this point I was three to six months into my learning process, depending on how you define it.  And though I had done a lot of work up to this point, the real learning didn't actually start until I began building the app (this app, Badge List).

Below are some chaotic notes from the building of Badge List.  I hope to come back and clean these up later.

Bootstrap

https://github.com/twbs/bootstrap/blob/master/less/variables.less >> Bootstrap color variables http://getbootstrap.com/2.3.2/components.html  >> Bootstrap documentation bootstrap sass variables: https://github.com/thomas-mcdonald/bootstrap-sass/blob/2.1-stable/vendor/assets/stylesheets/bootstrap/_variables.scss

Rspec Syntax

Note: The standard documentation is super annoying… looking for good examples / references http://blog.teamtreehouse.com/an-introduction-to-rspec

Maybe this? >> https://www.relishapp.com/rspec/rspec-core/docs >> nope… this one! >> https://www.relishapp.com/rspec/rspec-expectations/v/2-14/docs/built-in-matchers

More tips… http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

Open Badge Framework

Step 1 >> https://github.com/mozilla/openbadges/wiki/Assertions >> This is like the holy grail. It’s the metadata spec, super well written.
Step 2 >> https://github.com/mozilla/openbadges/wiki/Issuer-API >> This explains how to actually get the badge into your backpack

CarrierWave

This is the best file uploading thing to use… not sure if it supports non-image uploads. http://railscasts.com/episodes/253-carrierwave-file-uploads?view=similar >> yes it does support non-image files >> http://stackoverflow.com/questions/7344618/carrierwave-and-correct-file-extension-depending-on-its-contents

Devise

https://github.com/plataformatec/devise >> Better format here: http://devise.plataformatec.com.br/

Good news… devise now supports mongoid by default!! :D … here’s another guy’s 2011 post on the setup >> http://hafizbadrie.wordpress.com/2011/03/01/mongoid-and-devise-in-rails-3/

Adding a custom user view… http://stackoverflow.com/questions/7086583/creating-a-users-show-page-using-devise

Email

http://myrailslearnings.wordpress.com/2013/02/19/using-amazon-ses-to-send-emails-from-rails/ http://stackoverflow.com/questions/4798437/using-amazon-ses-with-rails-actionmailer

How to send and receive email in Rails >> http://guides.rubyonrails.org/action_mailer_basics.html
Picking a cloud email provider > Done!  I went with postmark primarily because they are 100% focused on transactional email..  I figure we can use another provider for marketing emails altogether.
http://developer.postmarkapp.com/

Tests

https://github.com/rspec/rspec-rails https://github.com/jnicklas/capybara https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

RSpec Generators

https://www.relishapp.com/rspec/rspec-rails/docs/generators
rails generate rspec:model widget >> spec/models/widget_spec.rb rails g rspec:controller width >> spec for the controller

What’s the difference between feature specs and other specs (request specs)? >> The railstutorial guy says he’s moving towards feature specs in future editions http://stackoverflow.com/questions/15173946/rspec-what-is-the-difference-between-a-feature-and-a-request-spec http://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
  • Starting with capybara 2.0 there are two separate folders…
    • spec/requests >> Do not include capybara by default. Best practices: use methods like get, post, put and delete and assert against response. Use describe / it
    • spec/features >> Use capybara and the visit method, usually assert against page. Use feature / scenario
Now I’m getting confused about how models & controllers fit into this… http://pivotallabs.com/testing-strategies-with-rspec/ >> meh, that last one was only passingly helpful given my current understanding level, perhaps it will make more sense later.  from the rspec documentatin here are the different types…
  • Model Specs
  • Controller Specs
  • Request Specs
  • Features Specs
  • These don’t seem as big...
    • View Specs
    • Helper Specs
    • Mailer Specs
    • Routing Specs
Capybara cheat sheet: https://gist.github.com/zhengjia/428105 Capybara rspec matchers >> http://rubydoc.info/github/jnicklas/capybara/master/Capybara/RSpecMatchers Capybara finders >> http://www.ruby-doc.org/gems/docs/c/capybara-rails-2-2-0.4.1.1/Capybara/Node/Finders.html

Controller Tests >> http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html

Simple Form 

Add it to the gem file and update $ rails g simple_form:install --bootstrap then it says to be sure to add the simple form tag into the erb along with a bootstrap form class ex >> simple_form_for(@user, :html => {:class => ‘form-horizontal’}) do |form|

documentation: http://simple-form.plataformatec.com.br/

great example of using simple form with bootstrap >> http://simple-form-bootstrap.plataformatec.com.br/articles/new

Initial Mongoid Setup

  • create the app
  • rails new app_name --skip-active-record
  • add this to the gemfile gem ‘mongoid' gem 'bson_ext' then run bundle update
  • create a scaffold for the badges >> rails g scaffold badge name image_url summary description

Mongoid Notes

http://mongoid.org/en/mongoid/docs/querying.html http://mongoid.org/en/origin/docs/selection.html >> NOTE: The DSL for querying actually comes from Origin >> http://mongoid.org/en/origin/docs/selection.html

Querying into arrays >> http://stackoverflow.com/questions/9392438/how-to-match-mongoid-documents-using-array-fields-in-query

http://webcache.googleusercontent.com/search?q=cache:VBHQkWWu7JUJ:blog.wiemann.name/mongoid-cheat-sheet+&cd=4&hl=en&ct=clnk&gl=us

For future reference: Carrierwave image uploading to mongoid / gridfs https://coderwall.com/p/lqtsya >> this is a good walkthrough, but… this is the generic / best place to start… https://github.com/carrierwaveuploader/carrierwave-mongoid

How to serve images from a database field http://stackoverflow.com/questions/4294427/rails-displaying-an-image-from-a-blob-field-in-a-database

Many to many relationships in mongoid?? Looks like this is not best practice? >> http://mongoid.org/en/mongoid/docs/relations.html http://codecraft.io/2011/07/31/mongoid-multiple-many-to-many-relations/ >> my exact use case!

Ruby Notes

Ruby and rails variable naming conventions >> http://itsignals.cascadia.com.au/?p=7

Finding syntax errors in a rake task… http://spectator.in/2010/02/19/ruby-1-dot-9-can-help-quickly-find-syntax-errors/ basically just try to compile it with the “w” option >> ruby -wc lib/finder.rb

Rails Notes

Temporarily turning off automatic rails timestamps
http://stackoverflow.com/questions/861448/is-there-a-way-to-avoid-automatically-updating-rails-timestamp-fields

Research 1: http://guides.rubyonrails.org/action_controller_overview.html >> http://guides.rubyonrails.org/layouts_and_rendering.html Research 2: I need to truly understand how ajax is supposed to work in rails.  

How to create a new app and specify the version… $ rails _2.1.0_ new myapp

when first using bundler be sure to do this >> bundle install --without production >> the w/o production part will be remembered in future bundle calls

also remember to update the gitignore file! (at least i think this is important)

add rspec / capybara right away then do this >> $ rails generate rspec:install >> configures app to use rspec instead of test::unit

Best practices for bulk data updating in a controller (in a restful manner) >> http://stackoverflow.com/questions/7965949/best-practice-for-bulk-update-in-controller

Time Zones

http://spilth.org/notes/rails3-date-time/ >> how to get a list of time zones… $ rake -D time
$ rake time:zones:all
$ rake time:zones:us

Environment Variables

http://railsapps.github.io/rails-environment-variables.html

Relationships

http://guides.rubyonrails.org/association_basics.html relationships in mongoid >> http://mongoid.org/en/mongoid/docs/relations.html

Semantic URLs / Vanity URLs / Understanding custom routing

This is so hard to find info on.
But I think this is it: http://jasoncodes.com/posts/rails-3-nested-resource-slugs >> NOPE… too complex, depends on the friendly url gem which is (a) overkill and (b) i believe doesn’t support mongoid


This is the best one: https://gist.github.com/cdmwebs/1209732 >> This does do a great job of explaining all the pieces. ++ >. This is also useful for reference …. http://railscasts.com/episodes/63-model-name-in-url-revised
  • First you have to nest the routes and use the :path parameter to allow them to be matched without the model name in the url
  • Then you have to override the to_param instance method
  • Then you have to either override self.find in the model OR add a validation method that implements an alternate find strategy
This is an important piece as well: http://stackoverflow.com/questions/10682346/friendly-urls-in-github

Two other ref links: http://apidock.com/rails/ActiveRecord/Base/to_param, http://stackoverflow.com/questions/13850819/can-i-determine-if-a-string-is-a-mongodb-objectid

Validating Semantic Profile URLs

http://edgeguides.rubyonrails.org/active_record_validations.html http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html >> this is actually the one that mongoid uses, though i think it’s the same validating from a list and including nil >> http://stackoverflow.com/questions/4730058/rails-validation-limit-input-to-specific-values Validating “vanity profile urls” This is the easy way >> http://stackoverflow.com/questions/9913064/how-can-you-make-sure-a-username-wont-conflict-with-an-existing-route

This looks like the better way to do it but sounds harder >> http://scribdtech.wordpress.com/2010/09/01/vanity-user-profile-urls-in-rails/ there’s also a gem that does super fancy stuff >> https://github.com/norman/friendly_id

UTF-8 Encoding

I had some problems storing the badge images directly in the MongoDB documents.  It turns out they were just problems with the encoding getting garbled.

http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/ ;

hankish Over 9 years ago

How I learned Rails - Part 2 - Developing a strategy.

After finishing the Rails tutorial I felt much more knowledgeable about Rails.  I also felt very ready to stop working on a fake app and start working on the real thing.

I spent a month or two developing wireframes and iterating them with feedback from my partner Ben and the potential users he was interviewing. Then I put together the first entity relationship diagram for the Badge List and tried to scope out a basic learning strategy.

My Self-Directed Learning Strategy

  1. Figure out what database and gems I wanted to use
  2. Learn about each gem
  3. Study up on ruby conventions
  4. Start coding the app!

hankish Over 9 years ago

How I learned Rails - Part 3 - Doing research and picking gems

I had no clue about this part.  I knew that I needed to figure out what the app was going to be shaped like, answering questions such as: 
  • What parts would be gems? / What parts would I code myself?  
  • What database would I use? / How would the database be structured?  
  • Where should I start coding?
What I didn't know was how to answer any of those questions with google.  So I phoned a very helpful friend who was also new to Rails, but was much farther down the path than I.  He had just launched a Rails-based project of his own.

Notes from call with friend

Source Control
Github for source control (worked with svn a while ago but git is even easier)

Bootstrap
The nice thing about bootstrap is
  • If you use it default, out of the box the html and css is super high quality.
  • It has a grid based layout system that’s easy
  • It’s almost automatically responsive
  • Alternatives to Bootstrap
    • Foundation: In the end they are really really similar. He went with bootstrap because it’s from twitter.  They are both really good frameworks and almost do exactly the same thing.  Very similar syntax.
    • There are other frameworks if you just want responsive or something else specific, but most of them do not have the same level of design assistance that bootstrap & foundation have.
Other recommendations for gems, frameworks & tools
  • For hosting he recommended Heroku to start
  • Rails for sure
  • Bootstrap
  • Backbone (optional: it makes the app nicer, but it adds a bit more complexity). You could also use the rails ERB view layer
  • Errors / ticket / bug tracking: RedMine (open source, ruby), Airbrake.io (captures errors in production and sends you an email)
  • Mobile apps:  
    • You need to have an API to do this, so that’s a good reason to go with the javascript client because it forces you to build the API in the beginning.
  • Amazon:
    • http://aws.amazon.com/rds/ >>; Hosted MySQL type data-base. Works really well. Takes care of redundancy and performance. (Heroku has their own mysql / postgres setup.)
  • Mysql vs. Postgres
    • Postgres is completely open source
    • Documentation is better than mysql
    • >> Has he looked into NoSQL / CouchDB? >> He tried to use it but didn't end up doing much with it. (He tried amazon’s dynamodb and also used mongodb.  Look at that if you’re going NoSQL).
      • >> Why use it? > You cannot use it for everything.  Sometimes a relational DB is better.  People use it a lot for high traffic applications.
      • If you use relational you might do a big join to figure out which posts are yours
      • But with nosql you can query an entire document (complete structure).
      • You might have redundant data
      • But accessing it is faster because you don’t have to do joins
      • He would say you could probably do the same thing with caching (like memcache)
    • Rails Gems
Authentication & Social Sign-in
There’s a good rails gem called devise (google: devise gem). It’s the most popular user management / authentication plugin for rails. It works well with the JS client architecture too, but it’s a great gem no matter what.  There are plugins for almost every oauth system you can find (google, facebook, etc)

Good learning resources?
RailsCasts is the best.  Paying for a Pro account is worth it. http://railscasts.com/

Best practices?
There are not many great resources for ralis best practices out there.  The books are ok, but usually they teach you to write unmaintainable ‘spaghetti code”.  
  • For instance: you will see lots of examples where lots of code is in the controller, but it’s often better to put more code into the model layer because it reduces code duplication. (Best practice: lean controller, fat model)
  • Use many gems.  For all of the basic, standard stuff there is a gem available and they’re all pretty good.  (Best practice: Don’t reinvent the wheel)
-----

More Research

After the conversation I did some more research on my own.

Backbone
http://stackoverflow.com/questions/5418369/what-is-the-purpose-of-backbone-jshttp://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/ ;
http://www.jamesyu.org/2011/02/09/backbone.js-tutorial-with-rails-part-2/ ;

>> Determination: Backbone seems too complicated, another thing to learn.  Skip it for now.

Bootstrap
http://railscasts.com/episodes/328-twitter-bootstrap-basics

SASS or LESS?

http://rubysource.com/twitter-bootstrap-less-and-sass-understanding-your-options-for-rails-3-1/ >>; Overview of my options for using bootstrap with rails (NOTE: A little outdated.. 2011)
http://css-tricks.com/sass-vs-less/ ;
>> this one is way better
>> MY DETERMINATION: Stick with SASS.  Both seem fine, but SASS is based in Ruby so I'm pretty safe in picking it.  Additionally, at this early juncture my own instinct is very raw, but the rails tutorial used SASS and that guy is an expert so that's good enough for me.

http://railscasts.com/episodes/329-more-on-twitter-bootstrap?autoplay=true>>; He recommends using simple form (which includes bootstrap support) to get the validation to work without messing up your markup >> More about simple form in episode 264

MongoDB
http://www.railstips.org/blog/archives/2009/12/18/why-i-think-mongo-is-to-databases-what-rails-was-to-frameworks/ >> Note: This one is kind of old (2009) so it's probably pretty out of date.
http://mongoid.org/en/mongoid/docs/installation.html
http://stackoverflow.com/questions/15512683/comparability-issue-rails-4-beta-ruby-2-0-0-mongoid

Heroku specific (MongoHQ vs Mongolab)
https://devcenter.heroku.com/articles/mongohq
>> Determination: Mongo lab has better support (http://webmasters.stackexchange.com/questions/20782/mongodb-hosting-mongolab-vs-mongohq-vs-mongomachine )
>> People on quora (http://www.quora.com/MongoDB/What-is-the-best-mongodb-hosting) also seem to prefer mongo lab .  Higher prices but more reliability or something.

Devise
http://railscasts.com/episodes/209-devise-revised
http://railscasts.com/episodes/235-devise-and-omniauth-revised >>; wow... enabling social sign-in is (while well documented) something of an undertaking. I'm revising this as not an MVP thing.  I'm also looking to investigate a question: What is the point of social sign in??
>> http://blog.mailchimp.com/social-login-buttons-arent-worth-it/

>> The takeaway: Meh... social sign in is DEFINITELY not needed for now. Even if we go for it later a question must be answered first: what is the specific goal of adding social sign in? What do we stand to gain?  How will it make our user experience better?  How will it help the users?

Ruby Conventions
How should I write my Ruby code in order to have it comply with community standards and make it pretty?
https://github.com/styleguide/ruby >>; They have a lot of other style guides too (CSS, Javascript, etc)
https://github.com/bbatsov/ruby-style-guide
Formatting Documentation: http://tomdoc.org/


The End Result - My Decisions

After many days of research and thinking, this is the initial design I went with:
  • Database: MongoDB - I was initially intrigued by not having to do migrations, but what clinched it for me was the argument that relational databases were designed before the advent of web apps and are not properly suited to the task.  It felt true to me: When I think about it there are so many limitations to having to get your data to "fit" into a relational DB.  With document-based databases you can store arrays and hashes and have the flexibility to add new fields at any time.  It just makes more sense.
    • MongoDB gem: Mongoid - It seemed like this gem was more modern.
    • MongoDB host company: MongoLab - This decision was based mostly on a response to a quora question.
  • User Authentication: Devise - Super popular, works with Mongoid.
  • CSS Framework: Bootstrap - Really actively developed, well used, compatible with lots of other gems. Great documentation.  Started by twitter which is a plus for me.
  • Testing Gem: RSpec - What I was used to after the Rails tutorial.

hankish Over 9 years ago