Skip to content

Instantly share code, notes, and snippets.

Routes

小心地使用 Match(Rails 3 已实现)

Rails 3 提供了 match 方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:

注:(r3 代表 Rails 3,r4 代表 Rails 4)

# routes.rb
@zernel
zernel / .zshrc
Created September 18, 2015 02:52
ZSH
# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh
# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="robbyrussell"
# Uncomment the following line to use case-sensitive completion.
@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 / 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 #不管有没有异常,进入该代码块