cheatsheets/_output/rails.html

154 lines
3.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<link href="style.css" rel="stylesheet" />
</head>
<body>
<h1>Rails</h1>
<h2>Helpers</h2>
<pre><code>class ApplicationController
helper_method :logged_in?
def logged_in?
&quot;Something&quot;
end
end
</code></pre>
<h3>CSS/JS packages</h3>
<pre><code>stylesheet_link_tag :monkey
javascript_link_tag :monkey
</code></pre>
<h3>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, &quot;First name&quot;
= f.text_field :first_name
= f.label :last_name&gt;
= f.text_field :last_name&gt;
- fields_for @person.permission do |fields|
= fields.checkbox :admin
-# name=&quot;person[admin]&quot;
- 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>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>Before filter</h3>
<pre><code>class ApplicationController &lt; 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] = &quot;You must be logged in to access this section&quot;
redirect_to new_login_url # halts request cycle
end
end
end
</code></pre>
<h3>Default URL optinos</h3>
<pre><code>class ApplicationController &lt; ActionController::Base
# The options parameter is the hash passed in to 'url_for'
def default_url_options(options)
{:locale =&gt; I18n.locale}
end
end
</code></pre>
<h3>Hashes</h3>
<pre><code>session[:what]
flash[:notice] = &quot;Your session expired&quot;
params[:id]
</code></pre>
<h3>XML and JSON</h3>
<pre><code>class UsersController &lt; ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml =&gt; @users}
format.json { render :json =&gt; @users}
end
end
end
</code></pre>
<h3>Redirection</h3>
<pre><code>redirect_to action: 'show', id: @entry.id
redirect_to root_url # a path
</code></pre>
<h3>Render</h3>
<pre><code>render nothing: true
render template: 'products/show'
render action: 'something' # same as `file: 'my/something'`
# Renders the template only, does not execute
# the action
</code></pre>
<h2>Layouts</h2>
<pre><code># app/views/layouts/application.html.erb
&lt;%= content_for?(:content) ? yield :content : yield %&gt;
# app/views/layouts/news.html.erb
&lt;% content_for :content do %&gt;
...
&lt;% end %&gt;
&lt;% render template: :'layouts/application' %&gt;
</code></pre>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script src="http://cachedcommons.org/cache/prettify/1.0.0/javascripts/prettify-min.js"></script>
<script>$("pre").addClass("prettyprint");</script>
<script>prettyPrint();</script>
</body>
</html>