match'/:year'=>"posts#index",:constraints=>{:year=>/\d{4}/,:ip=>/192\.168\.1\.\d{1,3}/}constraints(:host=>/localhost/)doresources:postsendconstraintsIpRestrictordo# Responds to self.matches?(request)get'admin/accounts'=>"queenbee#accounts"end
Legacy Route
1
match'/:controller(/:action(/:id(.:format)))'# Commented out by default
Scope
12345678910
scope':token',:token=>/\w{5}/do# Requires token parameter to get to resources and must by 5 alphanumeric charactersresources:roomsdoresources:meetingsendendscope'(:locale)',:locale=>/en|pl/do# Local is optional for these routesresources:postsroot:to=>'posts#index'end
$bundle'Make sure all dependencies in your Gemfile are available to your application. If they are not available, go install them.$ bundle --without 'Installseverythingexceptgemsincludedin.$bundle--deployment'Isolates all gems into vendor/bundle, requires up-to-date Gemfile.lock, use gems in vendor/cache if they exist. $ bundle check'ChecksifthedependencieslistedinGemfilearesatisfiedbycurrentlyinstalledgems.$bundleshow[gem_name]'Shows all libraries which are included by the Gemfile and their dependencies. If [gem_name] is given, shows where it is located in the filesystem. $ bundle open 'Opensthegemsourceinthedefaulteditor.$bundleupdate[gem_name]'Recreates Gemfile.lock and runs 'bundle' to install new dependencies. $ bundle package'Copiesallprojectgemstovendor/cache/--usethisifyoudon'twanttorelyonexternalserversfordeployment.
Gemfile Syntax
123456789101112131415161718192021
source"http://rubygems.org"gem"hpricot","0.6"gem"sqlite3-ruby",:require=>"sqlite3"gem"local_gem",:path=>"~/Sites/local_gem"gem"rails",:git=>"git://github.com/rails/rails.git"# Additional parameters# :branch => "branch_name"# :tag => "tag_number"# :ref => "ref_number"git"git://github.com/rails/rails.git"do# only these two gems will be fetched from the git repositorygem"railties"gem"active_model"endgroup:testdogem"webrat"end
@posts=Post.where(:published=>true).order(params[:order])@joe_posts=Post.where(:auther=>"Joe").includes(:comments).limit(10).all# .all forces query execution and returns an array, not a relation
find(id_or_array,options)# All options are now sent using the chain methodsfind(:first,options)find(:all,options)first(options)all(options)update_all(updates,conditions,options)
XSS PROTECTION & UJS
XSS Protection
12345
<%= @post.body # safe by default %><%=raw(@post.body)# unsafe %><%= link_to raw("<span class='cart'>#{h @user_input}</span>"), cart_path # only h @user_input escaped, not the spans %>
data-remotedata-confirmdata-methoddata-disable-with# Parsed by JavaScript drivers -- defaults to rails.js
Deprecated
1234567891011
link_to_remoteremote_form_forobserve_fieldobserve_formform_remote_tagbutton_to_remotesubmit_to_remotelink_to_functionperiodically_call_remote# If you need to use any of the deprecated helpers, visit http://github.com/rails/prototype_legacy_helper
classUserMailer<ActionMailer::Basedefwelcome(user,subdomain)# instance variables are available within your view@user=user@subdomain=subdomainmail(:from=>'admin@app.com',:to=>@user.email,:subject=>'Welcome')endend
classUserMailer<ActionMailer::Basedefault:from=>'admin@test.com',:reply_to=>'noreply@test.com',"X-Time-Code"=>Time.now.to_i.to_sdefwelcome(user,subdomain)@user=user@subdomain=subdomainattachments['test.pdf']=File.read(Rails.root.join('public/test.pdf'))attachments['photo.jpg']={:content=>generate_image()}mail(:to=>@user.email,:subject=>'Welcome to TestApp')do|format|# Defaults to: welcome.html.erb, welcome.text.erbformat.html{render'other_html_welcome'}format.text{render'other_text_welcome'}endendend
classPersonextendActiveModel::Callbacksdefine_model_callbacks:savebefore_save:action_before_savedefsave_run_save_callbacksdo# Your save action methods hereendendprivatedefaction_before_save# Your code hereendend