Introduction to Programming in Ruby on Rails
Learn to Program with Ruby
Leaders: JoshCo & Jack
See the app running on Heroku
http://npdev-people.herokuapp.com/
to start with my running example
Github repo with the running code: git@github.com:joshco/npdev-people.git
Perform the following steps:
git clone git@github.com:joshco/npdev-people.git people
bundle install
rake db:migrate
rails server
browse to http://localhost:3000
drink the rails kool-aid
doing it yourself
$ = command line prompt code italics = edits inside a text editor or IDE program comments about what/why this is important and does stuff = not defined yet
$ git commit -m “foo”
Rails is easier to teach and you don’t have to go into as many details as other languages
contact list:
model view controller - rails works with a model view controller paradigm
Contact List has a number of fields - name, email, etc.
the model is the table of people
the view ends up on the interface of the web browser (add, edit, view contacts)
Controller is the http server that will use the model and the view to know what to do
edit and add records in the model table
People is the name of the app
very easy with rails to create a new framework for your app
$ rails new people
- creates base app
$ rails generate scaffold person
- creates the model
$ rails generate scaffold person name:string email:string phone:string
- create base html files, reasonable looking view, create controller which will answer requests, creates a migration that is basically database entries
Rails knows how to pluralize things, so said people instead of persons (most of the time)
Many languages (especially PHP) there are a bazillion options/ways to do the same thing and rails is very strict with following conventions
$ rake db:migrate
- creates the migration file, table
rake = ruby version of make command
IDE - rubymine highly recommended and you can find the project folder to view the files that rails created
$ rails server
in browser go to: localhost:3000 /people - see a list of people
want to know about your gemfile - open the gemfile inside rubymine/IDE
Troubleshooting initial install:
comment out the gem ‘jquery-rails’ line (~23 in example) | # in front of line to comment out in rails
# gem 'jquery-rails'
and try to move gem ‘json-pure’,
gem ‘json-ruby’
if you have trouble initially
in browser: click on “new person” and enter fields to add a new person
Rails lets you get to so much with so much less coding
marriage hero started in march of this year with others helping and in less than a year had an event list, geocoded, and a map and almost as simple as adding the geocode to the gemfile and magic happens
after you create a new person, you can go and edit views and wraps
open config folder and find routes.db and open in editor type
root :to -> ‘people#index’
save and check out the browser again
app folder, views, layout, application.html.erb
2 types of views (layout displayed at all times and per object views)
the application.html.erb
in body
Welcome to NPDev
save and now it’s on every page
(cause it’s a banner) banners are the part that frame your application
look at controllers, people_controller.rb
.def index @people - person.all @foo - ‘barrrr’
respond_to do (asking for the format like html, .json)
if you want a rest API for your app, it’s already done for you with Rails
update and create render forms and you can find the “success messages”
edit apps\views\people\index.erb
go to bottom above link -> person <%= @foo %>
another example: <% if @foo==‘bar’ %> - for if statements/comparisons
create a git repository, add the files to the repository, do a commit/initial check-in
gemfile:
group :production do gem ‘pg’ end
group :development do
gem ‘sqlite3’
end This puts the production database that will be on heroku as postgres and the development DB as SQL lite (which is local and a smaller db)
$ heroku apps:create npdev people $ git remote -v $ git status $ git commit -m ‘db change’ $ git push heroku master
git pushes to heroku, but acts like a repository but with a lot of hooks that can detect rails, and can fire up the app assuming no errors
manually need to tell it to do a migration to create the database
you’ll have all these migrations for all models you created, and you can rollback if you screw up
$ heroku run rake db:migrate
creates the tables
then, once it’s done you should be able to view it live on heroku/browser link
$ heroku --tail logs
watch in command line as it spins up
an intended convention that keeps your local database not sync’d with your online database (production) without apps
you can later on do complicated queries when you advance and maintain a database sync’d where you pull down the database locally for development and testing