2017年7月13日 星期四

rails 撈數據庫轉array數組

User.all.map(&:id)  => [36, 37, 1, 2, 4, 6, 7, 8, 9, 10, 11, 14, 13, 15, 17, 18, 16, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 38, 12]

User.all.map(&:id).class => Array

rails map

哈希轉Array陣列
hash = { a:1, b:2, c:3 }
hash.map{|k,v| v} => [1, 2, 3] 

哈希數組
hash = { :a => 1, :b => 2, :c => 3 } <--覺得打字不順手
因为符号(Symbol)做為key的情况太普遍了,Ruby 1.9 干脆为这种用法定义了一种新句法:
也可以這樣寫 
hash = { a:1, b:2, c:3 }

k是key
v是value
在每個value裡面乘以2
hash.merge(hash) {|k,v| v*2 } => {:a=>2, :b=>4, :c=>6}

rails array移除掉nil

數組類型為Array 
["a",nil,"b"].class => Array 
 
移除掉空值 
["a",nil,"b"].compact => ["a", "b"]
 
沒有nil需要移除掉返回nil
["a","b"].compact! => nil 

2016年3月19日 星期六

open file vim plugin NERDTree

What's the "NERDTree"?

It's open file plugin by VIM.
http://www.flickr.com/photos/30496122@N07/2862367534/sizes/o/

How to install NERDTree
=========Create Folder============
cd ~
mkdir .vim
mkdir -p ~/.vim/autoload ~/.vim/bundle   #Create Folder "autoload" and "bundle"

=========Load files===============
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git

cd ~/.vim/autoload
curl -O https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim

=========Setting .vimrc============(If you don't have .vimrc...touch .vimrc)
execute pathogen#infect()
syntax on
filetype plugin indent on


=========Open NERDTree===========
Open VIM and type ":NERDTree"
or
vim ~/.vimrc
nnoremap <silent> <F5> :NERDTree<CR>
push F5 open NERDTree


Reference:
http://www.vim.org/scripts/script.php?script_id=1658
https://github.com/scrooloose/nerdtree
https://github.com/tpope/vim-pathogen
https://www.youtube.com/watch?v=-GdaZ-Fuk9w

2016年1月29日 星期五

postgresql accept tcp/ip connections on port 5432

Modify two files and Restart PostgreSQL
1.pg_hba.conf
+  host all all 0.0.0.0/0  password

file path:
/etc/postgresql/9.4/main/pg_hba.conf

2.postgresql.conf
Remove the mark
#listen_addresses = '*'
#post = 5432

file path:
/etc/postgresql/9.4/main/postgresql.conf

Tyep Command Line:
service postgresql restart

2016年1月14日 星期四

Rails JSON API

可以用三種方式讓瀏覽器讀取URL顯示JSON
下面這種是撈取member這個table所有資料
http://localhost:3000/api/member

下面這種是撈取member的第一筆資料,如果要撈第50筆資料把數字改成50就好
http://localhost:3000/api/member/1
...
http://localhost:3000/api/member/50

下面這種跟http://localhost:3000/api/member/1 一樣都是撈第一筆資料只是URL寫法不同而已
http://localhost:3000/api/member/1.json

Model:
$rails g model member name:string email:string

Controller:
$rails g controller /api member

member_controller.rb
def index
   @member = Member.all
    render json: @member
  end

def show
   @member = Member.find(params[:id])
   render json: @member
end

def create
  @member = Member.new()
  if @member.save
    render json: @member
  else
    render json: @member.errors, status: :unprocessable_entity
  end
end

def update
  if @member.update()
    render json: @member
  else
    render json: @member.errors, status: :unprocessable_entity
  end
end

def destroy
  @member.destroy
  head :no_content
end


Routes:
namespace :api do
    resource :members, only: [:index, :create, :show, :update, :destroy]
end

查詢資料
curl -i http://localhost:3000/api/member/

寫入資料
curl -i -X POST -H "Content-Type: application/json" -d '{"member": {"name":"Apple","email':"ABC@gmail.com"}}' http://localhost:3000/api/member

更新指定資料
curl -i -X PUT -H "Content-Type: application/json" -d '{"name":"Apple","email':"ABC@gmail.com"}}' http://localhost:3000/api/member/1

刪除指定資料
curl -i -X DELETE http://localhost:3000/api/member/1

2015年11月24日 星期二

ufw是Ubuntu專用的防火牆套件(推薦)

$sudo apt-get install ufw
$ufw enable
$ufw status

$ufw allow 3000   --開3000 port
$ufw status

$ufw allow 80   --開80 port
$ufw status

$ufw -v  可以看到所有指令

FATAL: role "root" does not exist

$sudo -u postgres createuser root



How to set postgres password in the beginning?

$sudo -u postgres psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'your new password';"




bundle install meet 'pg' issue for digitalocean

$rails new demo -d postgresql
$bundle install
An error occurred while installing pg (0.18.4), and Bundler
cannot continue.
Make sure that gem install pg -v '0.18.4' succeeds before
bundling.

Answer:
sudo apt-get install libpq-dev

2015年11月16日 星期一

How to install PostgresSQL in Ubuntu?


$sudo apt-get update
$sudo apt-get install postgresql-9.3

$sudo -i -u postgres
$psql
$\q




https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04
http://askubuntu.com/questions/50621/cannot-connect-to-postgresql-on-port-5432

2015年11月11日 星期三

Git Install and Use it

$sudo apt-get install git-core

$sudo mkdir /var/git

$cd /var/git
$sudo mkdir new_project.git

$cd new_project.git

$sudo git --bare init

$sudo git config core.sharedRepository true

Add Git Group
=======================
$sudo chgrp -R gitgroup *
$sudo adduser willy gitgroup
=======================


======================
$sudo chmod -R g+ws *
======================

$sudo chmod -R 777 .

ls -al

How to install Git User Interface in Ubuntu?

Install Git GUI
$sudo apt-get install git-gui

Run Git GUI
$git gui &