362 lines
9.4 KiB
HTML
362 lines
9.4 KiB
HTML
<!doctype html>
|
|
<html lang='en' class='no-js '>
|
|
<head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta charset='utf-8'>
|
|
<meta content='width=device-width, initial-scale=1.0' name='viewport'>
|
|
<link href='./assets/favicon.png' rel='shortcut icon'>
|
|
<meta content='/rails.html' name='app:pageurl'>
|
|
|
|
|
|
<title>Rails cheatsheet</title>
|
|
<meta content='Rails cheatsheet' property='og:title'>
|
|
<meta content='Rails cheatsheet' property='twitter:title'>
|
|
<meta content='article' property='og:type'>
|
|
|
|
|
|
|
|
<meta content='https://assets.devhints.io/previews/rails.jpg?t=20231011104129' property='og:image'>
|
|
<meta content='https://assets.devhints.io/previews/rails.jpg?t=20231011104129' property='twitter:image'>
|
|
<meta content='900' property='og:image:width'>
|
|
<meta content='471' property='og:image:height'>
|
|
|
|
|
|
|
|
<meta content="The one-page guide to Rails: usage, examples, links, snippets, and more." name="description">
|
|
<meta content="The one-page guide to Rails: usage, examples, links, snippets, and more." property="og:description">
|
|
<meta content="The one-page guide to Rails: usage, examples, links, snippets, and more." property="twitter:description">
|
|
|
|
|
|
<link rel="canonical" href="https://devhints.io/rails">
|
|
<meta name="og:url" content="https://devhints.io/rails">
|
|
|
|
|
|
|
|
|
|
|
|
<meta content='Devhints.io cheatsheets' property='og:site_name'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta content='Rails' property='article:section'>
|
|
|
|
|
|
|
|
|
|
|
|
<script async src='https://www.googletagmanager.com/gtag/js?id=G-N7TC6B227L'></script>
|
|
<script>
|
|
|
|
window.dataLayer=window.dataLayer||[];
|
|
function gtag(){dataLayer.push(arguments)};
|
|
gtag('js',new Date());
|
|
gtag('config','G-N7TC6B227L');
|
|
</script>
|
|
|
|
|
|
|
|
<meta property='page:depth' content='1'>
|
|
|
|
|
|
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
|
|
<script>(function(H){H.className=H.className.replace(/\bNoJs\b/,'WithJs')})(document.documentElement)</script>
|
|
|
|
<script>(function(d,s){if(window.Promise&&[].includes&&Object.assign&&window.Map)return;var js,sc=d.getElementsByTagName(s)[0];js=d.createElement(s);js.src='https://cdn.polyfill.io/v2/polyfill.min.js';sc.parentNode.insertBefore(js, sc);}(document,'script'))</script>
|
|
|
|
<!--[if lt IE 9]><script src='https://cdnjs.cloudflare.com/ajax/libs/nwmatcher/1.2.5/nwmatcher.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js'></script><script src='https://cdn.rawgit.com/gisu/selectivizr/1.0.3/selectivizr.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script><![endif]-->
|
|
|
|
<style>html{opacity:0}</style>
|
|
<link rel="stylesheet" href="./assets/2015/style.css?t=20231011104129">
|
|
<link href="./assets/style.css?t=20231011104129" rel="stylesheet" />
|
|
<link href="./assets/print.css?t=20231011104129" rel="stylesheet" media="print" />
|
|
</head>
|
|
<body>
|
|
<div class='all'>
|
|
|
|
<div class='site-header'>
|
|
<div class='container'>
|
|
This is <a href="."><em>Devhints.io cheatsheets</em></a> — a collection of cheatsheets I've written.
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<script type='application/ld+json'>
|
|
{
|
|
"@context": "http://schema.org",
|
|
"@type": "NewsArticle",
|
|
"mainEntityOfPage": {
|
|
"@type": "WebPage",
|
|
"@id": "https://google.com/article"
|
|
},
|
|
"headline": "Rails cheatsheet",
|
|
"image": [ "https://assets.devhints.io/previews/rails.jpg?t=20231011104129" ],
|
|
"description": "The one-page guide to Rails: usage, examples, links, snippets, and more."
|
|
}
|
|
</script>
|
|
<script type='application/ld+json'>
|
|
{
|
|
"@context": "http://schema.org",
|
|
"@type": "BreadcrumbList",
|
|
"itemListElement": [{
|
|
"@type": "ListItem",
|
|
"position": 1,
|
|
"item": {
|
|
"@id": "https://devhints.io/#rails",
|
|
"name": "Rails"
|
|
}
|
|
},{
|
|
"@type": "ListItem",
|
|
"position": 2,
|
|
"item": {
|
|
"@id": "https://devhints.io/rails",
|
|
"name": "Rails"
|
|
}
|
|
}]
|
|
}
|
|
</script>
|
|
|
|
|
|
<div class='post-list -single -cheatsheet'>
|
|
<div class='post-item'>
|
|
<div class='post-headline -cheatsheet'>
|
|
<p class='prelude'><span></span></p>
|
|
<h1><span>Rails</span></h1>
|
|
|
|
<div class='pubbox'>
|
|
<div class='HeadlinePub' role='complementary'>
|
|
<script async src='https://pubsrv.devhints.io/carbon.js?serve=CE7IK5QM&placement=devhintsio' id="_carbonads_js"></script>
|
|
<span class='placeholder -one'></span>
|
|
<span class='placeholder -two'></span>
|
|
<span class='placeholder -three'></span>
|
|
<span class='placeholder -four'></span>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class='post-content -cheatsheet'>
|
|
<h2 id="helpers">Helpers</h2>
|
|
|
|
<pre><code>class ApplicationController
|
|
helper_method :logged_in?
|
|
|
|
def logged_in?
|
|
"Something"
|
|
end
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="cssjs-packages">CSS/JS packages</h3>
|
|
|
|
<pre><code>stylesheet_link_tag :monkey
|
|
javascript_link_tag :monkey
|
|
</code></pre>
|
|
|
|
<h3 id="forms">Forms</h3>
|
|
|
|
<pre><code># http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
|
|
|
|
- form_for @person do |f|
|
|
= f.label :first_name
|
|
= f.label :first_name, "First name"
|
|
= f.text_field :first_name
|
|
|
|
= f.label :last_name>
|
|
= f.text_field :last_name>
|
|
|
|
- fields_for @person.permission do |fields|
|
|
= fields.checkbox :admin
|
|
|
|
-# name="person[admin]"
|
|
- fields_for :person, @client do |fields|
|
|
= fields.checkbox :admin
|
|
|
|
= f.submit
|
|
|
|
# Also: check_box, email_field, fields_for
|
|
# file_field, hidden_field, label, number_field, password_field
|
|
# radio_button, range_field, search_field, telephonen_field,
|
|
# text_area, text_field, url_field
|
|
</code></pre>
|
|
|
|
<h2 id="controllers">Controllers</h2>
|
|
|
|
<p>http://apidock.com/rails/ActionController/Base</p>
|
|
|
|
<pre><code>class ProjectsController
|
|
layout 'project' # Actually defaults to `projects` based
|
|
# on the controller name
|
|
|
|
def save
|
|
end
|
|
|
|
def edit
|
|
end
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="before-filter">Before filter</h3>
|
|
|
|
<pre><code>class ApplicationController < ActionController::Base
|
|
before_filter :validate, only: [:save, :edit]
|
|
before_filter :ensure_auth, except: [:logout]
|
|
|
|
before_filter :require_login
|
|
|
|
private
|
|
|
|
def require_login
|
|
unless logged_in?
|
|
flash[:error] = "You must be logged in to access this section"
|
|
redirect_to new_login_url # halts request cycle
|
|
end
|
|
end
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="default-url-options">Default URL options</h3>
|
|
|
|
<pre><code>class ApplicationController < ActionController::Base
|
|
# The options parameter is the hash passed in to 'url_for'
|
|
def default_url_options(options)
|
|
{:locale => I18n.locale}
|
|
end
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="hashes">Hashes</h3>
|
|
|
|
<pre><code>session[:what]
|
|
flash[:notice] = "Your session expired"
|
|
params[:id]
|
|
</code></pre>
|
|
|
|
<h3 id="xml-and-json">XML and JSON</h3>
|
|
|
|
<pre><code>class UsersController < ApplicationController
|
|
def index
|
|
@users = User.all
|
|
respond_to do |format|
|
|
format.html # index.html.erb
|
|
format.xml { render :xml => @users}
|
|
format.json { render :json => @users}
|
|
end
|
|
end
|
|
end
|
|
</code></pre>
|
|
|
|
<h3 id="redirection">Redirection</h3>
|
|
|
|
<pre><code>redirect_to action: 'show', id: @entry.id
|
|
redirect_to root_url # a path
|
|
</code></pre>
|
|
|
|
<h3 id="render">Render</h3>
|
|
|
|
<pre><code>render nothing: true
|
|
render template: 'products/show'
|
|
render status: 500
|
|
render status: :forbidden
|
|
render text: '...'
|
|
render layout: 'special_layout'
|
|
render layout: false
|
|
render action: 'something' # same as `file: 'my/something'`
|
|
# Renders the template only, does not execute
|
|
# the action
|
|
|
|
render json: object
|
|
render xml: object
|
|
|
|
render location: photo_url(photo)
|
|
</code></pre>
|
|
|
|
<h3 id="head-only-responses">Head-only responses</h3>
|
|
|
|
<pre><code>head :bad_request
|
|
head :created, location: photo_path(@photo)
|
|
</code></pre>
|
|
|
|
<h2 id="layouts">Layouts</h2>
|
|
|
|
<pre><code># app/views/layouts/application.html.erb
|
|
<%= content_for?(:content) ? yield :content : yield %>
|
|
|
|
# app/views/layouts/news.html.erb
|
|
<% content_for :content do %>
|
|
...
|
|
<% end %>
|
|
<% render template: :'layouts/application' %>
|
|
</code></pre>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<ul class="social-list ">
|
|
<li class="facebook link hint--bottom" data-hint="Share on Facebook"><a href="https://www.facebook.com/sharer/sharer.php?u=https://devhints.io/rails.html" target="share"><span class="text"></span></a></li>
|
|
<li class="twitter link hint--bottom" data-hint="Share on Twitter"><a href="https://twitter.com/intent/tweet?text=The%20ultimate%20cheatsheet%20for%20Rails.%20https://devhints.io/rails.html" target="share"><span class="text"></span></a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class="about-the-site">
|
|
<div class="container">
|
|
<p class='blurb'>
|
|
<strong><a href=".">Devhints.io cheatsheets</a></strong> is a collection of cheatsheets I've written over the years.
|
|
Suggestions and corrections? <a href='https://github.com/rstacruz/cheatsheets/issues/907'>Send them in</a>.
|
|
<i class='fleuron'></i>
|
|
I'm <a href="http://ricostacruz.com">Rico Sta. Cruz</a>.
|
|
Check out my <a href="http://ricostacruz.com/til">Today I learned blog</a> for more.
|
|
</p>
|
|
|
|
|
|
<p class='back'>
|
|
<a class='big-button -back -slim' href='.#toc'></a>
|
|
</p>
|
|
|
|
|
|
<p>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/highlight.min.js"></script>
|
|
|
|
|
|
|
|
|
|
<script src="https://cdn.rawgit.com/rstacruz/unorphan/v1.0.1/index.js"></script>
|
|
<script>hljs.initHighlightingOnLoad()</script>
|
|
<script>unorphan('h1, h2, h3, p, li, .unorphan')</script>
|
|
|
|
</body>
|
|
</html>
|
|
|