Skip to content

Instantly share code, notes, and snippets.

@zernel
zernel / bootstrap_form_builder.rb
Created August 22, 2012 15:33 — forked from shamil614/bootstrap_form_builder.rb
Twitter Bootstrap 2.0 Form Builder & Devise
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
delegate :capture, :content_tag, :tag, to: :@template
%w[text_field text_area password_field collection_select email_field].each do |method_name|
define_method(method_name) do |name, *args|
errors = object.errors[name].any?? " error" : ""
error_msg = object.errors[name].any?? content_tag(:span, object.errors[name].join(","), class: "help-inline") : ""
content_tag :div, class: "control-group#{errors}" do
@zernel
zernel / Gemfile
Created August 24, 2012 01:45
Devise authentication with multiple provider
gem 'devise'
gem 'omniauth'
gem 'omniauth-github'
gem "omniauth-twitter"
gem "omniauth-facebook"
gem "omniauth-google-oauth2"
@zernel
zernel / gist:3444718
Created August 24, 2012 02:06
OmniAuth Google OAuth2 strategy – where to get Google Key and Secret
* Go to https://code.google.com/apis/console, log in with your Google account and click on API Access.
* Create OAuth2 credentials by clicking the big blue OAuth2 button.
* Set the callback url and the name of your app (can be edited later)
* The Google Key (as it is referred to by OmniAuth) is the Client ID
* The Google Secret (as it is referred to by OmniAuth) is the Client Secret
* Use those credentials as per your documentation for implementing OAuth2 authentication.
The Google OmniAuth strategy for OAuth2 is here: https://github.com/zquestz/omniauth-google-oauth2. Look in the examples folder for details on the rest of the implementation.
@zernel
zernel / flash.html.erb
Created August 27, 2012 03:08
Flash List Tips
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<a class="close" data-dismiss="alert">×</a>
<%= msg %>
</div>
<% end %>
@zernel
zernel / gist:3720010
Created September 14, 2012 05:40
Backup the database
导出数据库pg_dump -Fc mydb > db_name.dump
导入数据库 pg_restore -d mydb db_name.dump
导出 pg_dump mydb > db_name.sql
表导出 pg_dump database -t table > table.sql
加上' -a '可只到导出表内容,不导出表结构
@zernel
zernel / gist:3749856
Created September 19, 2012 14:03
Logger
file = File.join(Rails.root, 'log/markdown.log')
File.new(file, 'w+') unless File.exist?(file)
logger = Logger.new(file)
logger.info("#{Time.now}: Share##{_id}'s price is from #{latest_price} to #{new_price}")
logger.close
@zernel
zernel / apache2
Created September 20, 2012 04:35
nginx/apache2 裸域名跳转
<VirtualHost *:80>
ServerName anderweb.ca
RewriteEngine on
RewriteRule ^(.*)$ http://www.anderweb.ca$1 [R=301,L]
</VirtualHost>
@zernel
zernel / init git repos
Created September 26, 2012 07:11
Git tips
1 服务器上:
2 git init --bare --shared reggin.git
3 本地:
4 git remote add origin xxx@git.xxx.xx:/home/dev/repos/xxxx.git
@zernel
zernel / gist:3807297
Created September 30, 2012 16:12
Ruby 异常处理
begin #开始
raise.. #抛出异常
rescue [ExceptionType = StandardException] #捕获指定类型的异常 缺省值是StandardException
$! #表示异常信息
$@ #表示异常出现的代码位置
else #其余异常
..
ensure #不管有没有异常,进入该代码块
@zernel
zernel / smser.rb
Created October 20, 2012 10:32 — forked from mimosz/smser.rb
短信宝
# -*- encoding: utf-8 -*-
require 'digest/md5'
require 'nestful'
class Smsbao
def initialize(login, passwd)
@login = login
@passwd = Digest::MD5.hexdigest(passwd.to_s)
end