Rails 3 提供了 match 方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:
注:(r3 代表 Rails 3,r4 代表 Rails 4)
# routes.rb| source/_includes/custom/head.html中添加js代码: | |
| <script> | |
| function addBlankTargetForLinks () { | |
| $('a[href^="http"]').each(function(){ | |
| $(this).attr('target', '_blank'); | |
| }); | |
| } | |
| $(document).bind('DOMNodeInserted', function(event) { |
| # 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, |
| #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) |
| #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) |