Your First SaaS Applicatoin

Part I: Get RottenPotatoes running locally (or from your c9 workspace)

The actual RottenPotatoes starter app you will use is in repo and please read the README.md file carefully for more guidance:

https://git.trustie.net/cgao/hw-rottenpotatoes-rails-intro.git

Whenever you start working on a Rails project, the first thing you should do is to run Bundler, to make sure all the app’s gems are installed.
Switch to the app’s root directory (presumably rails-intro) and run $ bundle install --without production(you only need to specify–without production the first time, as this setting will be remembered on future runs of Bundler for this project).
If there is an error information on installing gems, edit source to ‘http://ruby.taobao.org’ in Gemfile.
Finally, get the local database created:

rake db:migrate

Self-check: how does Rails decide where and how to create the development database? (Hint: check the db sub directory)
This creates a local development database and runs the migrations to create the app’s schema. It also creates the file db/schema.rb to reflect the latest database schema. You should place this file under version control.

Self-check: what tables got created by the migrations?
…And insert “seed data” into the database–initial data items that the app needs to run:

rake db:seed

Self-check: what seed data was inserted and where was it specified? (Hint:rake -T db:seed explains the seed task;rake -Texplains other available Rake tasks)

At this point you should be able to run the app locally ($ rails server) and navigating to http://localhost:3000 in your browser.

Notice: You should also have a repository in your Trustie platform.

Part II: Deploy to Heroku

If you have deployed to Heroku before, just create a new app container with heroku create. If this is your first time deploying to Heroku, you will need to do two things.
First, sign up for a free Heroku account. Then set up ssh keys to securely communicate with Heroku for app deployments.
The three basic commands you need are the following, but see the Heroku page for more details if following commands fail to work.

1
2
3
$ ssh-keygen -t rsa
$ heroku login
$ heroku keys:add

Once your keys are set up (a one-time process), you should be able to create an “app container” on Heroku into which you’ll deploy RottenPotatoes:

$ heroku create

Heroku will assign your app a whimsical name such as luminous-coconut-237; once your app is deployed, you would access it at http://luminous-coconut-237.herokuapp.com. You can login to the Heroku website if you want to change the name of your app.
Finally, we deploy our app to Heroku:

$ git push heroku master

(It is normal to see the following warning the first time—answer “yes”, and in the future you shouldn’t see it any more:)
The authenticity of host ‘heroku.com (50.19.85.132)’ can’t be established.
RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad.
Are you sure you want to continue connecting (yes/no)?
Please type ‘yes’ or ‘no’:
Is the app running on Heroku? No, because just as we ran rake db:setup to do first-time database creation locally, we must also cause a database to be created on the Heroku side:

$ heroku run rake db:setup

Now you should be able to navigate to your app’s URL. heroku open opens your browser to that URL in case you forgot it.
Once you’re confident the functionality works correctly on Heroku, submit the URI of your deployed Heroku app in a text file with no other contents.
Please be careful to use http and not https, that is, submit http://your-app.herokuapp.com and NOT https://your-app.herokuapp.com.

完成步骤

Part I

安装Ruby on Rails及相关环境

见之前的文章。

本地运行项目

在一个合适的目录下:
$ git clone https://git.trustie.net/cgao/hw-rottenpotatoes-rails-intro.git

然后$ bundle install --without production,根据提示信息进行相关的操作,保证相关的包都安装好。

运行:

1
2
3
$ rake db:migrate
$ rake db:seed
$ rails server

在执行$ rails server时,可能运行不成功,并提示系统使用的rails版本和项目要求的rails版本不相符,则需要使用rvm对ruby和rails的版本配合使用进行控制,详细见上一篇文章。

C9.io运行项目

相对本地运行项目,在C9上运行项目则要方便很多,省去了环境的配置这个繁琐的步骤。

1.配置好Cloud9环境

2.在C9的终端内,将项目克隆到开发环境中:
$ git clone git@github.com:saasbook/rottenpotatoes-rails-intro.git

3.进入git项目目录:
$ cd rottenpotatoes-rails-intro

4.运行$ bundle install --without production,保证该项目所需要的所有软件包都安装好了。

5.运行$ bundle exec rake db:setup,创建初始数据库。

6.运行$ rails server -p $PORT -b $IP来运行项目。项目运行起来后,Cloud9将弹出一个窗口告知项目的运行URL,可通过浏览器进行项目访问。

Part II

部署到Heroku

1.终端登录Heroku:

1
2
3
4
5
$ heroku login
Enter your Heroku credentials:
Email: katherineleeyq@gmail.com
Password: *********
Logged in as katherineleeyq@gmail.com

根据提示输入帐号信息。

2.向Heroku添加SSH:
$ heroku keys:add
根据提示,新创建或者直接向Heroku添加SSH Key。

3.现在已经登录了Heroku,通过:

1
2
3
$ heroku create
Creating app... done, ⬢ radiant-bastion-66073
https://radiant-bastion-66073.herokuapp.com/ | https://git.heroku.com/radiant-bastion-66073.git

在Heroku服务器上创建一个新项目(并不是创建在本地)。Heroku将随机为这个项目命名,但是你可以进行修改。

4.进入Part I本地运行项目时的项目git目录下:
$ heroku git:remote -a radiant-bastion-66073
将heroku的项目地址添加到remote中。

5.将项目部署到Heroku:
$ git push heroku master

6.现在直接访问项目地址,Heroku页面将提示项目错误,因为此时项目只是部署上去了,还没有运行起来,通过:
$ heroku run rake db:setup
即可运行项目。