Thursday, May 21, 2015

Ruby on Rails Note - 05/20/2015

1. css files are in app/asset/stylesheets/xxx.scss. Rails keeps application.html.erb as a framework page for the entire application. The application.html.erb file is located in views/layouts/ directory. In order to use the xxx.scss, it has to be introduced in the application.html.erb file, in such way as following.
  <body class='xxx.scss'> or <body class='<%= controller.controller_name%>'>
Then, the css classes can be used in index.html.erb.

2. Some rails helper function:
(1) cycle('a', 'b') sets the css class of each row to either 'a' or 'b' and automatically toggles between the two style names on successive lines.
(2) trunctate(string, length:n) is used to display just the first n characters of string.
(3) strip_tags(string) removes the html tags from the string.
(4) link_to('Product', @product) is equal to
     link_to('Product', controller:'products', action:'show', id:product)
     The link_to generates  a hyper link which execute show method in controller which invokes show.html.erb if nothing in the method.

3. Git usage
(1) basic configuration
    $ git config --global --add user.name "Alex"
    $ git config --global --add user.email alex@gmail.com
    $ git config --global --list
These configuration info will be added every check in as the committer info.
(2) add a .gitignore file to specify a file list that you don't want to check in
(3) create a local empty repository in current directory.
    $ git init
(4) add all the files under the current directory to local repository.
    $ git add .
(5) commit the change to local repository
    $ git commit -m "first commit"

If you want to clear the local repository.
(1) rm -rf .git

If you have a github account, you can push your repository to the github
(1) create a repository on github
(2) $ git remote add origin "YOUR HTTPS GITHUB REPOSITORY URL"
(3) $ git push origin master




Tuesday, May 19, 2015

Ruby on Rails Note - 05/19/2015

1. To create a new project.
    $ rails new [project name]

2. Create a scaffold. A scaffold is a full set of model, database migration for that model, controller to manipulate it, view to view and manipulate the data, and a test suite for each of the above.
    $ rails generate scaffold Product \
            title:string description:text image_url:string price:decimal

The default database for Rails development is SQLite3. The config/database.yml file tells Rails where to look for the database.

3. To migrate database.
    $ rake db:migrate

The migration file is xxxxxxxxxxxxxx_create_xxxx.rb. A migration represents a change we want to make to the data, expressed in a source file in database-independent terms. These changes can update both the database schema and the data in the database tables.

4. To start rails server.
    $rails server

5. To import seed data to the database.
    $rails db:seed

The data can be defined in db/seed.rb.