OSDN Git Service

Regular updates
[twpd/master.git] / rails.md
1 ---
2 title: Rails
3 category: Rails
4 ---
5
6 Helpers
7 -------
8
9     class ApplicationController
10       helper_method :logged_in?
11     
12       def logged_in?
13         "Something"
14       end
15     end
16
17 ### CSS/JS packages
18
19     stylesheet_link_tag :monkey
20     javascript_link_tag :monkey
21
22 ### Forms
23
24     # http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
25
26     - form_for @person do |f|
27       = f.label :first_name
28       = f.label :first_name, "First name"
29       = f.text_field :first_name
30
31       = f.label :last_name>
32       = f.text_field :last_name>
33
34       - fields_for @person.permission do |fields|
35         = fields.checkbox :admin
36
37       -# name="person[admin]"
38       - fields_for :person, @client do |fields|
39         = fields.checkbox :admin
40
41       = f.submit
42
43     # Also: check_box, email_field, fields_for
44     # file_field, hidden_field, label, number_field, password_field
45     # radio_button, range_field, search_field, telephonen_field,
46     # text_area, text_field, url_field
47
48 Controllers
49 -----------
50
51 http://apidock.com/rails/ActionController/Base
52
53     class ProjectsController
54       layout 'project'   # Actually defaults to `projects` based
55                          # on the controller name
56
57       def save
58       end
59
60       def edit
61       end
62     end
63
64 ### Before filter
65   
66     class ApplicationController < ActionController::Base
67       before_filter :validate, only: [:save, :edit]
68       before_filter :ensure_auth, except: [:logout]
69
70       before_filter :require_login
71      
72       private
73      
74       def require_login
75         unless logged_in?
76           flash[:error] = "You must be logged in to access this section"
77           redirect_to new_login_url # halts request cycle
78         end
79       end
80     end
81
82 ### Default URL options
83
84     class ApplicationController < ActionController::Base
85       # The options parameter is the hash passed in to 'url_for'
86       def default_url_options(options)
87         {:locale => I18n.locale}
88       end
89     end
90
91 ### Hashes
92
93     session[:what]
94     flash[:notice] = "Your session expired"
95     params[:id]
96
97 ### XML and JSON
98   
99     class UsersController < ApplicationController
100       def index
101         @users = User.all
102         respond_to do |format|
103           format.html # index.html.erb
104           format.xml  { render :xml => @users}
105           format.json { render :json => @users}
106         end
107       end
108     end
109
110 ### Redirection
111
112     redirect_to action: 'show', id: @entry.id
113     redirect_to root_url          # a path
114
115 ### Render
116
117     render nothing: true
118     render template: 'products/show'
119     render status: 500
120     render status: :forbidden
121     render text: '...'
122     render layout: 'special_layout'
123     render layout: false
124     render action: 'something'    # same as `file: 'my/something'`
125                                   # Renders the template only, does not execute
126                                   # the action
127
128     render json: object
129     render xml: object
130
131     render location: photo_url(photo)
132
133 ### Head-only responses
134
135     head :bad_request
136     head :created, location: photo_path(@photo)
137
138 Layouts
139 -------
140
141     # app/views/layouts/application.html.erb
142     <%= content_for?(:content) ? yield :content : yield %>
143
144     # app/views/layouts/news.html.erb
145     <% content_for :content do %>
146        ...
147     <% end %>
148     <% render template: :'layouts/application' %>
149