Skip to content

Instantly share code, notes, and snippets.

View jay16's full-sized avatar

俊杰.li jay16

View GitHub Profile

Routes

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

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

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

# routes.rb
#Model
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
response.should render_template(:index)
#Redirecting
response.should redirect_to(movies_path)
@jay16
jay16 / my_db.rake
Last active February 8, 2023 09:27 — forked from rantoniuk/backup.rake
rails backup DB
#encoding: utf-8
require "fileutils"
namespace :my_db do desc "Backup project database. Options: DIR=backups RAILS_ENV=production MAX=7"
desc "usage - bundle exec rake my_db:backup RAILS_ENV=production MAX=15 DIR=db/db.bak"
task :backup => [:environment] do
# config base dir
datestamp = Time.now.strftime("%Y%m%d%H%M")
base_path = Rails.root
backup_folder = File.join(base_path, ENV["DIR"] || "backups")
FileUtils.mkdir_p(backup_folder) unless File.exist?(backup_folder)
@jay16
jay16 / lisp.rb
Created April 10, 2014 13:12 — forked from dahlia/lisp.rb
# 30 minutes Lisp in Ruby
# Hong Minhee <http://dahlia.kr/>
#
# This Lisp implementation does not provide a s-expression reader.
# Instead, it uses Ruby syntax like following code:
#
# [:def, :factorial,
# [:lambda, [:n],
# [:if, [:"=", :n, 1],
# 1,
source/_includes/custom/head.html中添加js代码:
<script>
function addBlankTargetForLinks () {
$('a[href^="http"]').each(function(){
$(this).attr('target', '_blank');
});
}
$(document).bind('DOMNodeInserted', function(event) {
#!/usr/bin/env ruby
# Libraries:::::::::::::::::::::::::::::::::::::::::::::::::::::::
require 'rubygems'
require 'sinatra/base'
require 'slim'
require 'sass'
require 'coffee-script'
# Application:::::::::::::::::::::::::::::::::::::::::::::::::::

最近在看Sinatra 1.3.5的源码。在Sinatra::Helpers模块中看到一个名为uri的方法,用来将一个相对路径转换为绝对路径。在该函数的代码中看到分别针对request.script_name和request.path_info进行了处理。但是并不清楚这两个参数的区别,于是做了一些reseach。

二者在RFC3875中有如下定义:

4.1.13. SCRIPT_NAME

The SCRIPT_NAME variable MUST be set to a URI path (not URL-encoded) which could identify the CGI script (rather than the script's output). The syntax is the same as for PATH_INFO (section 4.1.5) >

@jay16
jay16 / gist:280b67d7cbd8603806b7
Last active April 12, 2016 23:26 — forked from anonymous/gist:c6b2b4c7f9673c60108c
qlikview marco export EXCEL
Sub ExcelPrint
Call exportToExcel_ALL()
End Sub
Public Function exportToExcel_ALL()
Dim aryExport(14,3)
aryExport(0,0) = "CH57"
aryExport(0,1) = "直营业绩周报表"
aryExport(0,2) = "A1"
aryExport(0,3) = "data"
@jay16
jay16 / goto-test.rb
Last active August 29, 2015 14:06 — forked from alexbevi/goto-test.rb
require 'goto'
def test
frame_start
label(:start) { goto :b }
label(:a) { print "world!\n"; goto :c }
label(:b) { print "hello "; goto :a }
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')