Yu-Chieh’s Blog (Y.C. Chang)

Ruby on Rails / Rubygems / FullStack / Git / Mac notes.

How to Setup for Ssh Connet to Vps Server

  • use ssh-keygen to generate keys (public/private)
1
$ ssh-keygen -t rsa # -t refers to key type, here we use rsa
  • The keys were generated under ~./ssh folder
  • copy public key to vps server
1
2
$ cd ~/.ssh
$ cat id_rsa.pub
  • Sign in vps server
  • Before we use ssh to connect vps, we have to activate ssh service for ssh connection.
1
2
3
4
5
6
7
$ sudo apt-get install openssh-server # make sure your server install openssh service (here we use ubuntu)
$ sudo pico /etc/ssh/sshd_config # edit sshd setting
# Port 12141 # do not forget to change port for security reason
# PermitRootLogin yes --> PermitRootLogin no # for disable root ssh login 
$ sudo /etc/init.d/ssh restart # restart sshd
# or use service tool
$ service ssh restart
  • put your public key to vps
1
2
3
$ mkdir .ssh
$ touch authorized_keys
# then copy your local ~/.ssh/id_rsa.pub to authorized_keys by any text editor (vi,pico...etc)
  • On Mac, we can use ‘ssh-copy-id’ to install our identity.pub in a remote machine’s authorized_keys.
  • Now, you can ssh to connect your vps directly without typing any password.
  • enjoy.

How to Put Css in Html Body?

  • There are two ways to set css file into our html documents.
  • first way: using pure javascript to append css file into head part of html.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<script type="text/javascript">

   function loadCSS(filename){

      var file = document.createElement("link")
      file.setAttribute("rel", "stylesheet")
      file.setAttribute("type", "text/css")
      file.setAttribute("href", filename)

      if (typeof file !== "undefined")
         document.getElementsByTagName("head")[0].appendChild(file)
   }


  //just call a function to load a new CSS:
  loadCSS("path_to_css/file.css")

</script>

Converting File to Utf8 Encode by Iconv

  • Sometimes, we need to convert file from different encode to utf8 before we may open and edit it. iconv command make it easy.
1
2
# convert input_file from big5 encode to utf8 encode
$ iconv -f BIG5 -t UTF8 input_file > output_file
  • done.

How to Install Paperclip for Rails App on Windows

  • If we want to run rails app with paperclip in windows environment, we need to install two package for it.

  • GnuWin32 - http://gnuwin32.sourceforge.net/packages/file.htm

  • ImageMagick binary- http://www.imagemagick.org/download/binaries/

  • Next, we should install the correct static version for your OS (32-bit vs 64-bit) (not dll version)

  • Finnally, we need to add the bin directory to your environment path (in config/environment/development.rb or production.rb) as following lines:

1
2
3
4
...
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin; C:\Program Files\ImageMagick-6.7.6-3-Q16'
Paperclip.options[:swallow_stderr] = false
...

How to Install Ionic and Its Dependencies

  • install node js by Homebrew
1
2
3
4
$ brew doctor
$ brew update
$ brew install node
$ brew install ant
  • install cordova and ionic by npm
1
2
$ npm install -g cordova ionic
$ npm install -g ios-sim

Install Lavish on Ubuntu 14.04

  • Lavish is a Bootstrap theme generator by Quan. It can generate both CSS and LESS code from an image you provide. Then, you can customize it.

  • Recently, I want to install on my ubuntu server for using it locally and conviniently.

  • the following steps are my install howto:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git clone https://github.com/mquan/lavish.git
$ cd lavish/
$ rvm install 1.9.3
$ rvm use 1.9.3
$ bundle install --without production

$ sudo apt-get install libmagickwand-dev # for solving rmagick install problem

$ sudo add-apt-repository ppa:chris-lea/node.js # optional
$ sudo apt-get -y update # optional

$ sudo apt-get -y install nodejs # for ExtJS

$ rails s # open http://localhost:3000/ and enjoy it.
  • done.

Enable Ssh in Ubuntu

  • for ubuntu 14.04, we need to install OpenSSH Server
1
$ sudo apt-get install openssh-server
  • next, we might want to configure your ssh service
1
2
# edit sshd config file
$ sudo nano /etc/ssh/sshd_config
  • finally, restart it.
1
$ sudo /etc/init.d/ssh restart
  • done.

Deploy Ruby on Rails App on Ubuntu 14.04 by Mina

  • Mina is fast and easy deploy tool for ruby on rails app (especially faster than capistrano). This note shows how to deploy ruby on rails app by mina.

  • Start up an ubuntu server (here we use Ubuntu 14.04 LTS x64 on vultr cloud server)

  • ssh to login your server

1
$ ssh username@123.123.123.123

How to Generate Email View and Send It Out Within Rails Application

  • If we want to send email within rails app, we could use generator for creating mailer class and view.
1
$ rails g mailer NewsMailer # NewsMailer is the name of mailer class
  • After that, we could get generated files under app folder.
1
2
3
4
5
6
  create  app/mailers/news_mailer.rb
  create  app/mailers/application_mailer.rb
  invoke  erb
  create    app/views/news_mailer
  create    app/views/layouts/mailer.text.erb
  create    app/views/layouts/mailer.html.erb
  • Next, we need to edit mailer class (news_mailer.rb contains empty mailer so we need to edit it.)
1
2
3
4
5
6
7
8
9
class NewsMailer < ApplicationMailer
  default from: 'news@demo.com'

  def news_email(user, news)
    @user = user
    @news = news
    mail(to: @user.email, subject: @news.title)
  end
end

How to Install and Use Ckeditor and Paperclip in Rails App

  • In paperclip requirement, Paperclip must have access to ImageMagick that must be installed. For Mac OS X, we could run this command with Homebrew.
1
$ brew install imagemagick
  • add this to your rails app Gemfile.
1
2
gem 'paperclip'
gem 'ckeditor'
  • then bundle it.
1
$ bundle
  • next, we should generate model for paperclip to store uplaoded files
1
$ rails generate ckeditor:install --orm=active_record --backend=paperclip