Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Monday, June 10, 2013

Ruby on Rails tutorial

I began this tutorial, http://ruby.railstutorial.org/ruby-on-rails-tutorial-book, about a year ago and got through a few chapters.  However, I lost momentum, and for reasons that I do not remember at all, I stopped working on Ruby development.  Since I got a new computer and I probably forgot everything that I learned, I'm going to have to start from scratch and go through the tutorial again from the beginning.

Installing ruby, rvm, gem, rails ...
There seems to be a lot of stuff needed to install and configure in order for you to begin coding.  There really isn't anything fun or interesting to learn as you install this software.  You just have to do it, and hope that you didn't make some silly mistake that comes back to bite you later.   After a few minutes of downloading, compiling and other magic, I finished the installation process.

first_app
So after installing and configuring, you can start up your first app rather quickly.  With these commands:
rvm use ruby-1.9.3-p429  
rails new first_app
bundle update
bundle install
rails server
Bam! The server was up and running for me, viewable from a broswer and I didn't have to write a single line of code. It was almost too easy.

Github and Heroku
As recommended in the tutorial I also made a git repository, pushed the code onto github and deployed the project to heroku.  There is a lot of 'pushing' and 'pulling', and it can be a little confusing.  Here is a rundown of what's going on.

On my local machine (my laptop), I have all the source code and configuration needed to run the application.  This is good for development.  If all of your users (and other developers) had access to your laptop, this might be good enough.

But what if I want to share my code or back it up in case my machine crashes?  That's where GitHub comes along.  At the lowest level, Github is a place to back up your code.  At a higher level, it's a repository which can help you keep track of changes, allow you to easily test and revert changes,  and share and merge your changes with yourself or others.  The final thing that Github does (that I will mention) is that it helps me push my files onto Heroku.

Now, what is Heroku?  Well, for the purposes of this first app I just made, it's a better version of your laptop.  After you push your latest versions of your files onto Heroku, it figures out that you have a ruby on rails app and starts the server, where users can now visit.  You can configure heroku to work a bit differently than your laptop.  For example: (if you followed the tutorial), on your laptop you used sqlite as the backend database, but told Heroku to use another database (postgres).

Conclusion
This is the second time I made the first_app ruby on rails app.  It's been so long, it was almost all new to me.  Although, I think I was able to move along a bit faster this time, and I definitely understood a bit more than I did last time around.

Sunday, February 5, 2012

Rails cheat sheet

#  create a new project (the -T flag means, don't make the test directory)
    rails new new_project_name -T

#  to include 'gems' to be installed for your app
    modify the Gemfile
    bundle update
    bundle install

#  do an install with a flag to skip some things
    bundle install --without production

#  create a resource, called "User" with attributes "name" and "email" of type string
    This will create all the MVC parts.
    rails generate scaffold User name:string email:string

#  The user Model is defined in this file.
#  This is similar to a C++ class definition (it even inherits from something, ActiveRecord::Base)
#  You can further tailor your model by adding validation constraints or
#  or create relationships between this model and others
    app/models/user.rb

#  The user Controller is defined in this file.
#  This looks like a list of event handlers that can affect your users model or view.
    app/controllers/users_controller.rb

#  The user View is defined in this file.
#  This gives info on how to display the page.
    app/views/users/index.html.erb

#  If you don't generate with a scaffold, this creates just a model, called "Model_name" with attribute1 and attribute2 of type string without the controller or view.
    rails generate model Model_name attribute1:string attribute2:string

#  If you don't generate with a scaffold, this creates just a controller, called "controller_name" that will control page "page_name"
    rails generate controller controller_name page_name

#  update the database structure after generate controller or generate user ...
    bundle exec rake db:migrate

#  undo a database update (the previous command)
    bundle exec rake db:rollback

#  get more help for database tasks
    bundle exec rake -T db

#  turn on the rails server
    rails s
    or (rails server)

#  start interactive rails environment
    rails console

#  location of resource pages [after generating the user resource and adding it to our database]
    /users   -> list of users
    /users/1  -> list of user with id == 1
    /users/new  -> place to make new users
    /users/1/edit  -> place to edit user with id == 1

#  create an integration test
    rails generate integration_test test_name
    - creates a test file under test_name_spec.rb

#  run test files (that are in directory spec/)
    bundle exec rspec spec/

#   create rspec tests
#   this creates files like spec/spec_helper.rb
     rails generate rspec:install

Thursday, February 2, 2012

A ruby on rails tutorial

I have been following this http://ruby.railstutorial.org/ruby-on-rails-tutorial-book, and it's pretty good.

After the first chapter, you will have created a simple application, saved it to github, and published it on heroku.

After the second chapter, you will use 'scaffold' to quickly create a rails application, create models that are backed by databases, and add some logic to define the relationship between the models.  This is done with git version control and publishing onto heroku.

After the third chapter, you will create a set of pages written in html using ruby.
You will build this page with test-driven development and will again check in the code to git hub and you publish onto heroku.

After the fourth chapter, you will continue development of the application made in the third chapter. You will add usage of css style sheets, with minimal changes to the application.  The majority of the chapter is spent explaining how objects are used in ruby and rails.

After the fifth chapter, you will have made a web page that has internal links to navigate within your application. At this point, you have a basic framework to build a basic web page.  At the same time, you will have created a page that may contain lots of elements and features that you do not fully understand how to use.

Sunday, January 29, 2012

Ruby on Rails for zombies

http://railsforzombies.org/
This is a site containing a video lesson of ruby on rails followed by a series of lab exercises that can be  done within the browser.  The video and exercises are very polished, well thought out, and relatively painless.

After completion, you will:
  learn the syntax and idioms of the language.
  be immersed in the philosophy of designing apps the 'rails' way.

Thursday, January 26, 2012

Ruby on rails (RoR)

(From the perspective of an absolute beginner)
It looks like a programming/scripting language that is designed to retrieve (from files and databases) and present (on websites and mobile applications) data.
It is used in some very popular applications, like Twitter.
It seems to have a religious, cult-like following who try to convert non-believers into users.

First taste
Googling for some info about this language (or framework or whatever) led me to: tryruby.org.  This site was mildly entertaining and it served its purpose of enticing me to learn more about RoR.  Actually, I liked the embedded command line prompt in the browser, so assuming that ruby was used to build that site, it seemed like RoR was something worth learning.

Installation
I'm following this tutorial:
http://coding.smashingmagazine.com/2011/06/21/set-up-an-ubuntu-local-development-machine-for-ruby-on-rails/
It looks like I need to install and use git.  I've read a bit about this version control system, and it sounds quite intimidating.  I'm excited to learn about it, which must make me a bit of a nerd.
In the end, it looks like I've installed:

git = open source distributed version control system
rvm = ruby version manager
ruby =  dynamic programming language
gem = ruby packaging system
rails = a framework to build web apps with ruby
capistrano = tool for running scripts on multiple servers
postgresql = database management system

This seems like a lot of stuff to get going.