Thursday, January 14, 2016

Chapter 3

This is the start of creating a micro-blogging site.
First, the usual process of:
  • rails new
  • (edit Gemfile)
  • bundle install --without production
  • bundle update
  • git init
  • git add -A
  • git commit -m "Initialize repository"
  • (edit readme file, rename, commit change)
  • (the below commands create a new bitbucket repository)
    • git remote add origin git@bitbucket.org:<username>/sample_app.git
    • git push -u origin --all
  • (add in hello by adding hello function to application_controller file and adding "root application#hello")
  • git commit -am "Add hello"
  • heroku create
  • git push heroku master
  • (create a new branch)
    • git checkout master
    • git checkout -b static-pages
  • rails generate controller StaticPages home help
  • (add newly created files to repository)
    • git add -A
    • git commit -m "add a static pages controller"
    • git push -u origin static-pages
  • bundle exec rake test (checks that tests added automatically pass)
  • (add about test)
    • test "should get about" do \\ get :about \\ assert_response :success \\ end
  • (To ensure test passes)
    • add a line in routes/config.rb: get 'static_pages/about'
    • add a line in static pages controller: def about // end
    • "touch app/views/static_pages/about.html.erb"
      (create an about.html.erb page in app/views/static_pages/)
    • add html to about page
  • (Systematic steps to prevent DRY: Don't repeat yourself)
    • as a learning step, to see that things must be done repetitively without it, rename application.html.erb (i.e. rename page template)
    • for each of the erb pages, add <% provide(:title, "preferredtext") %> at the top. and add <%= yield(:title) %> in the original title page.
    • rename back application.html.erb, and edit it to include the html, title, head tags (which should be deleted from the other erb files), as well as the <%= yield(:title) %> line
    • Edit config/routes.rb to have "root 'staticpages#home'" (remember to delete existing root)
  • (the usual)
    • git add -A
    • git commit -m "finished static pages"
    • git checkout master
    • git merge static-pages
    • git push
    • bundle exec rake test
    • git push heroku

Tuesday, January 12, 2016

Chapter 1

  • Using a cloud integrated development environment (cloud IDE) such as Cloud9 saves time by sidestepping multiple configuration and installation issues.
  • In Cloud9, Rails needs to be installed by the command "gem install rails"
  • Once Rails has been installed, you can use "rails new appname" to create a new app named appname.
  • In this case, one needs to edit the Gemfile and run "bundle install" to install some needed Gems that were not installed by default. (Even though bundle install was already run as part of the rails new command)
  • "rails server -b $IP -p $PORT" then creates a local web server.
  • One can then edit the controller in application_controller.rb to adjust what the program displays by default.
    • For example, once can define a function "hello" which renders text "Hello, World!"
    • Thereafter, go to config/routes.rb and add "root application#hello" to ensure that the function "hello" within the "application_controller.rb" file is performed when there is a request to the root route (or root URL). Notice that application is the controller name, and hello is the function.
  • One can then use Git for version control.
    • git init
    • git add -A (add new files)
    • git commit -m "I made some edits" (commits changes)
      • When no new files are created, one may replace the last two commands with git commit -a -m "I made some edits"
  • One can then set up Bitbucket and copy the SSH key from Cloud9 to Bitbucket
    • git remote add origin git@bitbucket.org:<username>/hello_app.git
    • git push -u origin --all
  • Don't forget to set up branches when experimenting.
    • git checkout -b modify-readme (creates branch called modify-readme)
    • (make edits)
    • git checkout master
    • git merge modify-readme
    • git branch -d modify-readme (deletes the branch; use only after you've tidied up)
    • git push (pushes to Bitbucket)
  • Since Heroku uses PostgreSQL, we need to add the pg gem into the production environment to allow Rails to talk to Postgres. We also need rails_12factor to serve up static images and stylesheets.
  • Then use "heroku create" to create a heroku app, as well as "git push heroku master" to push the master branch up to heroku.
  • Use the "heroku rename" to rename the website if you want a prettier name.

Welcome to this blog

This blog is a summary of Michael Hartl's online book, www.railstutorial.org. It is neither created nor endorsed by Mr Hartl.