[Ting's笔记Day8]活用套件carrierwave gem:(3)Deploy图片上传功能到Heroku网站

前情提要:git

 

身为Ruby新手村民,创造稳定且持续的学习步调很重要,我用的方法就是一周在IT邦写三篇笔记,但愿藉由把笔记和遇到的bug记录下来的过程,能帮助到将来想用Ruby on Rails架站的新手。:)github

 

话说我在这一天[Ting's笔记Day4]将Ruby on Rails项目部署到Heroku架好了站,并在此篇[Ting's笔记Day7]活用套件carrierwave gem:(2)利用Amazon S3架设图片服务器完成个人餐厅数据库网站的图片上传功能。接着就要把功能发布到Heroku啦!数据库

 

1.修改app/uploaders/image_uploader.rb服务器

为了节省Amazon S3用量,(毕竟是免费功能,储存空间必定的限制,能省则省!)app

我想在本机项目上传图片时,照片存在本机端就好(xcsjbj);学习

在Heroku(远端production版本)上传图片的时候,再上传至Amazon S3。网站

 

我能够去app/uploaders/image_uploader.rb修改一下代码从:rest

 

storage:fog图片

改为rem

 

if Rails.env.production?

storage:fog #Heroku,Use Amazon S3

else

storage:file

end

2.把Figaro gem加入Heroku

在上篇文章提到了Figaro帮咱们隐藏application.yml档,作好保护密钥的功能,因为这个功能动到了config,~~这件事情的严重性(?)~~咱们必需要让Heroku知道!

 

跟随着Figaro的关于Deployment的说明档指示,在ternimal输入figaro heroku:set -e production,程序就会把密钥信息传给我在heroku的正式环境。

 

tingdeMacBook-Air:yelpdemo tingtinghsu$ figaro heroku:set -e production

Setting aws_access_key_id,aws_secret_access_key,fog_directory and restarting⬢tingsrailsdemo…done,v7

aws_access_key_id: #秘密

aws_secret_access_key: #不告诉你

fog_directory: #Amazon S3 bucket

3.把全部改动的代码加入github版控,并push上Heroku:

在这篇文章里我已经对于git使用的熟练熟练了:

1.git status

2.git add .

3.git commit -m“增长照片上传功能”

4.git push

因此咱们先把写好的的功能推上github,最后用git push heroku master完成最后一哩路!

 


tingdeMacBook-Air:yelpdemo tingtinghsu$ git push heroku master

Counting objects: 59,done.

Delta compression using up to 4 threads.

Compressing objects: 100%(52/52),done.

Writing objects: 100%(59/59),331.27 KiB | 0 bytes/s,done.

Total 59(delta 33),reused 0(delta 0)

 

remote: https://tingsrailsdemo.herokuapp.com/ deployed to Heroku

remote:

remote: Verifying deploy…done.

To https://git.heroku.com/tingsrailsdemo.git

cb86240..05af5ff master -> master

到Heroku后台查看,新功能已经部署成功~

 

4.把新更动过的的餐厅图片数据库字段加入Heroku

咱们当初改完上传功能时,已经在本地端使用rake db:migrate让数据库顺利运行,详见[Ting's笔记Day6]活用套件carrierwave gem:(1)在Rails实现图片上传功能

但远端的Heroku部分还没呢!

所以记得:heroku run rake db:migrate更新Production版本的数据库。

 

这里Heroku出现错误信息Etimeout: connect etimeout 50.19.103.36:5000

参考这里的解法,才发现个人wifi所在地的防火墙锁port 5000。

 

我使用run:detached这个指令来解决此问题:heroku run:detached rake db:migrate

 

tingdeMacBook-Air:yelpdemo tingtinghsu$ heroku run:detached rake db:migrate

Running rake db:migrate on⬢tingsrailsdemo…done,run.2219(Free gongzuofs

Run heroku logs --app tingsrailsdemo --dyno run.2219 to view the output.

=大功告成=

 

试试网站上的上传功能,图片顺利存进Amazon S3:)

相关文章
相关标签/搜索