Skip to content

Instantly share code, notes, and snippets.

View speed-of-light's full-sized avatar

Speed of Light speed-of-light

View GitHub Profile
@speed-of-light
speed-of-light / Prevent nokogiri warning
Last active December 20, 2015 22:29
rails gem operations
gem install nokogiri -- --with-xml2-include=/usr/include/libxml2/libxml \
--with-xml2-lib=/usr/lib64/ \
--with-sxlt-include=/usr/include/libxslt \
--with-xslt-lib=/usr/lib64/
@speed-of-light
speed-of-light / gcst :: git clone specific tag
Last active December 21, 2015 10:08 — forked from fajrif/gist:1265203
shell string manipulation
function gcst(){
# $1: repo path
# $2: tag-name
git clone $1
cd ${$1%%.*}
git tag -l
git checkout $2
git branch -D master
git checkout -b master
@speed-of-light
speed-of-light / color_routes.rake
Last active December 21, 2015 15:49
rails rake color routes
desc 'Pretty version on rails rake routes.'
# modified from https://github.com/nicooga/color_routes
# put this file under your [rails.root]/lib/tasks/
# tested under rails 4.0.0
# Default usage:
# $>>> rake color_routes
# Routes with specific controller
# $>>> rake color_routes controller=devise/sessions
# Routes with multiple filters
# $>>> rake color_routes path=login verb=GET
@speed-of-light
speed-of-light / install_pg.sh
Last active December 21, 2015 16:09
Setup postgresql under ubuntu 13.04
# install something
sudo apt-get install postgresql postgresql-contrib
# check login ability
sudo -u postgres psql
# create user with passwords in shell
sudo -u postgres createuser $UserName -P
# try login as the created user

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname

Render and Redirect

The normal controller/view flow is to display a view template corresponding to the current controller action, but sometimes we want to change that. We use render in a controller when we want to respond within the current request, and redirect_to when we want to spawn a new request.

Render

The render method is very overloaded in Rails. Most developers encounter it within the view template, using render :partial => 'form' or render @post.comments, but here we'll focus on usage within the controller.

:action

@speed-of-light
speed-of-light / ability.rb
Last active December 23, 2015 02:39 — forked from vmarcetic/ability.rb
cancan ability
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # This is used for not logged user if you have a need for it
if User.current_role == 'admin' # From ApplicationController we can get current_role and check it up against the role we want.
can :manage, :all
else
# Guide
# Configure the essential configurations below and do the following:
#
# Repository Creation:
# cap deploy:repository:create
# git add .
# git commit -am "initial commit"
# git push origin master
#
# Initial Deployment:

Github 發 Pull Request & 貢獻流程速查

前言

此文目標讀者需先自行學會

  • 開 Github 帳號
  • 會 fork 程式 repository
  • 會在自己的電腦使用命令列 git
  • 會 clone 自己的 repository
@speed-of-light
speed-of-light / session.rb
Last active December 30, 2015 08:49 — forked from marcomd/gist:3129118
rails session controller for angular js
#Session controller provides a token
#/controllers/api/sessions_controller.rb
class Api::SessionsController < Devise::SessionsController
before_filter :authenticate_user!, :except => [:create]
before_filter :ensure_params_exist, :except => [:destroy]
respond_to :json
def create
resource = User.find_for_database_authentication(:email => params[:user_login][:email])
return invalid_login_attempt unless resource