Monday, February 13, 2012

Creating a Basic RoR Application

Creating a basic Ruby on Rails app to learn Jquery and other javascript. (using rails 3.0.6)
$ rails new learning_java
# update Gemfile to include Heroku
$ git init
$ git add .
$ git commit -m "initial commit"
# update README
$ git mv README README.markdown
$ git commit -a -m "Improved the README"
# create new repository on github: https://github.com/<username>
$ git remote add origin git@github.com:<username>/learning_java.git
$ git push origin master


As a result, have a new app, learning_java, on my local machine as well as on github.
Although it's early in the design phase, let's deploy to Heroku.

$ heroku create
$ git push heroku master
[Update 4/21/12]
If you receive the following error during the initial push to Heroku, do a bundle install and then repeat the push to git and push to heroku.
Error: "You have modified your Gemfile in development but did not check the resulting snapshot (Gemfile.lock) into version control"

# Website for app:
http://radiant-river-4002.heroku.com/

# make sure everything is up to date:
$ git push
$ git push heroku

# Checkout a new Git branch to start creating the app's pages
$ git checkout -b basic-pages

# Create the pages controller
$ rails generate controller Pages home about contact blog flightprojects hackings

Some background on controllers, classes and methods courtesy of The RoR Tutorial:
The pages_controller.rb defines a class called PagesController. Classes are simply a convenient way to organize functions (also called methods) like the home and contact actions, which are defined using the def keyword.

The PagesController is a Ruby class, but because it inherits from ApplicationController the behavior of its methods is specific to Rails: when visiting the URL /pages/home, Rails looks in the Pages controller and executes the code in the home action, and then renders the view (the V in MVC).

# Although we haven't done much, we can commit to github
$ git add .
$ git commit -m "Added a Pages controller"

Updated app/controllers/pages_controller.rb file.  Want to assign instance variables for each page method to define the page's title (@title = "Home").

The at sign @ in @title indicates that it is an instance variable. Instance variables have a more general meaning in Ruby, but in Rails their role is primarily to link actions and views: any instance variable defined in the home action is automatically available in the home.html.erb view, and so on for other action/view pairs.

Then, the app/views/layouts/application.html.erb can be updated to include embedded ruby defining the title.
More info on this here:
http://ruby.railstutorial.org/chapters/static-pages#sec:layouts

# Update the config/routes.rb file to map and name the routes:
# Replace the get statements in the routes file with these match commands...

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  root :to => 'pages#home'
# update the repository to remove the default index.
$ git rm public/index.html
$ git commit -am "Removed default Rails page"

Going to start venturing off the reservation soon, so want to push what we've got so far in case need to revert back.
Dump of command line history...
  503  git add .
  504  git commit -m "Done with basic page setup"
  505  git checkout master
  506  git branch
  507  git merge basic-pages
  508  git push
  509  git push heroku

OK, now for the new stuff.

Was going to install jrails, but saw a post that it was abandoned with Rails 3.  So going to use the jquery-rails gem.
Found this website as a good resource for installing the gem.

Add to the gemfile:
gem "jquery-rails"

# Then:
 $  bundle install
 $  rails generate jquery:install

Files magically appear in public/javascripts/


***

Messing with isotope and jquery
Downloaded isotope (a jquery plugin for "magical" layouts) from https://github.com/desandro/isotope

Looking at a friend's site that uses itotope and jquery.  Here are the files/scripts references...
     <script type="text/javascript" src="js/jb_olio.js"></script>
     <script type="text/javascript" src="js/jquery-1.6.2.js"></script>
     <script type="text/javascript" src="js/backbone/underscore.js"></script>
     <script type="text/javascript" src="js/jquery.scrollTo.js"></script>
     <script type="text/javascript" src="js/jquery.localscroll.js"></script>
  <script src="js/jquery-1.5.1.min.js"></script>
  <script src="jquery.isotope.min.js"></script>


Found this gem gem "jquery-scrollto-rails":
Added to Gemfile and run bundle install.  Got an error:

Bundler could not find compatible versions for gem "railties":
  In snapshot (Gemfile.lock):
    railties (3.0.6)

  In Gemfile:
    jquery-scrollto-rails depends on
      railties (~> 3.1)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.


Ran bundler update and didn't work.  Didn't find anything online, so going to remove from Gemfile for now.

Created the jquery.scrollTo.js and jquery.localscroll.js files by hand and put into javascript directory with other jquery files.

Can't get any of the js to render.  Tried this in application.html.erb, but also didn't work.
    <%= javascript_include_tag 'jquery', 'bj_folio' , 'jquery.min', 'jquery.toscrollTo', 'jquery.isotope.min', 'jquery.localscroll' %>

Basically, stuck right now.  Don't have any of the js working.

No comments:

Post a Comment