In this series I will create a simple web application. It includes the following main components:
- Mostly static pages, use bootstrap
- Users can signin, signout, remember user signin
- Home page show all users, update their profiles, admin users can delete other users.
- User can following or unfollow other users
- Users can post articles, update and destroy their posts.
- Users login and make their comments on articles.
- Home page show all articles and current user profile, paging.
- Build url for article
- Simple search articles
- Type ahead, use gon gem.
Ok, start!
Part I: Create new project, configure database use MySQL, modify Gemfile and then push it on Github, deploy on heroku server
Firstly, install ruby, rails framework on your system
Install Ruby on Linux and WindowsInstall Rails Framework on Linux and Windows
Test install ruby on rails
# ruby -v # rails -v
Create a new project
# mkdir projects # cd projects # rails new sample_app -d mysql
Modify Gemfile and configure database.yml
Open database.yml and change:username: root password: [your password] database: [database name]Ex:
default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: root socket: /var/run/mysqld/mysqld.sock development: <<: *default database: sample_app_development test: <<: *default database: sample_app_test production: <<: *default database: sample_app_productionOpen Gemfile and add some gem you want, following is sample Gemfile
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.0.rc1' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 1.2' group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.1.2' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano', group: :development # Use debugger # gem 'debugger', group: [:development, :test] group :development, :test do gem 'mysql2' gem 'web-console' gem 'spring' end group :test do gem 'minitest-reporters' gem 'mini_backtrace' gem 'guard-minitest' end group :production do gem 'pg' gem 'rails_12factor' end gem 'mocha', group: :testThen run command to install gem
# cd sample_app # bundle install --without production
Run on localhost
# cd sample_app # rails sOpen browser, http://localhost:3000 or http://
Push on github
Login on github and create an account, create a new project on github# cd sample_app # git init # git remote add origin git@bitbucket.org:<username>/sample_app.git # git add -A # git commit -m "First commit, Initialize project." # git push origin master or # git push -u origin --all
Push project on heroku
Login on heroku, create an account if you don't have. Heroku only use git to deploy rails application.Add hello world
Open application_controller.rb and add following lines:
def hello render text: "Hello, world!" endOpen routes.rb and add:
root 'application#hello'Update your changes to github
# git status # git add -A # git commit -m "Add hello" # git push -u origin master
# heroku login # heroku create # git push heroku masterPush other brand
# git push heroku new-brand:masterMay you need!
# git push heroku master -f # git push heroku new-brand:master -fIf you want to remove remote link heroku and add new link
# git remote -v # git remote rm heroku # heroku git:remote -a herokuappnameIf you want to remove current git remote link and add new link
# git remote -v # git remote rm origin # git remote add origin remote linkOpen web application on heroku
# heroku openDONE! Part02 to be continue.
rubyonrails

0 comments:
Post a Comment