154 lines
3.4 KiB
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?
|
|
"Something"
|
|
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, "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>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 < 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>Default URL optinos</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>Hashes</h3>
|
|
|
|
<pre><code>session[:what]
|
|
flash[:notice] = "Your session expired"
|
|
params[:id]
|
|
</code></pre>
|
|
|
|
<h3>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>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
|
|
<%= content_for?(:content) ? yield :content : yield %>
|
|
|
|
# app/views/layouts/news.html.erb
|
|
<% content_for :content do %>
|
|
...
|
|
<% end %>
|
|
<% render template: :'layouts/application' %>
|
|
</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>
|