Skip to content

Instantly share code, notes, and snippets.

@dfang
dfang / mirrors
Last active March 11, 2022 15:48
Mirrors in China
# mirrors in China
## rubygems mirror(ruby.taobao.org)
use ruby.taobao.org as mirror on local machine(https://ruby-china.org/topics/15534)
`bundle config mirror.https://rubygems.org http://ruby.taobao.org`
`less ~/.bundle/config`
## npm mirror(npm.taobao.org)
https://cnodejs.org/topic/4f9904f9407edba21468f31e
http://riny.net/2014/cnpm/
@dfang
dfang / gist:68f5b7667965f54dbe5c
Last active August 29, 2015 14:17
passing arguments to rake/cap task
@dfang
dfang / gist:a58bc901c7b584cdbafc
Last active August 29, 2015 14:10
use backup and whenever with capistrano to backup database on vps
1. install bakcup and whenever gem
```
gem 'backup'
gem 'whenever'
```
2. capistrano task(for uploading config file, put it to lib/capistrano/tasks/backup.rake)
```
namespace :backup do
@dfang
dfang / Termfile example
Last active August 29, 2015 14:08
Termfile example
directory = '~/Development/Work/gart360-web'
url = 'http://gart.dev:3000'
before { run "cd #{directory}" }
run 'fs'
tab do
run "tlog"
end
# Speed things up by not loading Rails env
config.assets.initialize_on_precompile = false

Rails Models

Generating models

$ rails g model User

Associations

belongs_to

has_one

@dfang
dfang / gist:d3ecefe66da294340026
Created May 22, 2014 08:27 — forked from dhh/gist:2492118
split a large routes.rb to multiple files
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
draw :api
draw :account
draw :session

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

mkdir -p /usr/local/etc/nginx/sites-{enabled,available}

File locations:

  • nginx.conf to /usr/local/etc/nginx/
  • default and default-ssl to /usr/local/etc/nginx/sites-available
  • homebrew.mxcl.nginx.plist to /Library/LaunchDaemons/
@dfang
dfang / my-sublime-preferences.sublime-settings
Created December 2, 2013 17:46
my sublime preferences
{
"always_show_minimap_viewport": true,
"binary_file_patterns":
[
"*.jpg",
"*.jpeg",
"*.png",
"*.gif",
"*.ttf",
"*.woff",