1. Your controller is only used to map URL (including some other HTTP requests), and the data model and view your template cascaded together, the results of their feedback to the HTTP response.
2. In practice, very important point to remember about your application, but is different in various parts of Ruby code, and when the code is loaded into Ruby parser in What do after the Tamen.
3.reload! Re-loading the console
4. In the controller action is not specified, if anything, its default behavior is to render the same name attempts than their corresponding template that each controller of the methods implies a render command, which render the same time is a real way.
def index render :template => "demo/index" end
5. If a controller's action do not want to render the default template, then call the render method to specify the rendering of any other template in app / views of any of the templates are to be rendered. (In fact, the entire project in any one view is that can be rendered.)
6. (1) render a different template if you can render in a different template in action, you can: template or: file an action in the rendering parameters in any template file specified.
**: Template template parameter to specify a relative path to the root directory, the default is the app / views (to modify)
**: File parameter to receive the absolute path to a file
render :template => "abuse/report" app/views/abouse/report.rhtml render :file => "/railsapps/myweb/app/views/templates/common.rhtml"
(2) rendering of local template (partial rendering)
You can make use of partial rendering the template into small files, so the code was not subject into confusion, the same time, the local template can be reused
(3) rendering inline template code similar to the code sent to the browser view of the function a major cause of Ajax for Rails view helper method.
render :inline => "<%= auto_complete_result(@headings, 'name') %>"
(4) rendering a text browser, if you only need to return a simple text, such as in response to an Ajax request, or other application system interface calls, you can render and return a text.
render :text => 'Submission accepted'
7. Render command may accept a series of options to return a specific data structure, by appropriately setting the response content-type, can be easily automated processing.
JSON is specifically provided to the JS data structures used in lightweight, which is usually the response of the Ajax request returns through this format. (Note: ActiveRecord has built-in support for JSON, so Rails is in dealing with JSON convenient)
render :joson => @record.to_json
ActiveRecord also built-in support for XML format
render :xml => @record.to_xml
8. What is not rendering
rendre :nothing => true, :status => 401
9. Rendering properties
(1): content_type used to verify whether you actually sent the contents of your use of the: content_type specified type
(2): layout template specifies whether the layout and the use of which layout template
(3): status according to their own state select the appropriate status code
Common status codes
307 temporary redirect (request for a temporary transfer of resources to another URL)
Prohibit access to 403 (service area understand the request, but refused to return)
404 Not Found (server can not find the requested resources required)
503 Service Unavailable (service is temporarily unavailable)
301 permanent directional
302 temporary directional
303 said to another place to see (a 303 oriented will tell the client, no matter what your original use of movement, first send a GET request to the new address)
307 temporary redirect (the original use and the same action to the new address send a request)
8. Whether rendering the default or specify a template, or other localized templates, text or other things, rendering is the final processing of a request. Redirection to the means to terminate the current request, and then start another new request . (Note: If your logic is complex, after the completion of rendering you can use redirection or return to end the process, to avoid DoubleRenderError error)
def show @user = User.find(params[:id]) if @user.activated? render :action => 'activated' and return end end
9.Rails pass the controller and view layer data using instance variables. Typically, the initialization method in the controller one or more instance variables, then the corresponding view template can be used .
10. Filtering device to perform an action controller Zai methods include the action before or after. Its Yibanyonglai in to perform action Qian for legalization cache management or other authentication Chu Li. Filter by Hong Ge Shi, they appear in your control device class definitions to the top, before the definition of the method. (and filters in order to highlight the nature of the decorative pattern, usually omitted in the method of using the filter after the brackets)
(1) can pass to filter any number of symbols (before_filter: security,: audit,: compress)
(2) can be split into single
before_filter :security before_filter :audit before_filter :compress
(3) can not be in the controller to before_filter and after_filter naming methods to create filters
(4) The filter should be set by calling the methods protected or private, so as to avoid public actions and they are to be executed.
(5) Sometimes you may need special control of the filter is the implementation of the order, in this case, you can use prepend_before_filter and prepend_after_filter, use this method to be added to the filter, the filter will be added to the beginning of the queue will also be the first execution.
(6) in a class specified in the filter is effective for its sub-class, but sometimes, some subclasses may need to skip the specified parent class of filters
(7) can be specified to filter the applicable conditions or the need to exclude the two methods can specify a single method (: only =>: index) or an array (: except => [: foo,: bar])
11. In addition to playing outside trying to template, Rails to send streaming files built to the browser to return to function (in the Rails built in two ways to do this thing, ActionController:: Streaming module: send_data and send_file)
(1) send_data (data, options = ())
send_data method allows you to specify the name of the user to send text or binary data
: Filename to the browser to provide a file name
: Type specifies the content type of HTTP, the default is application / octetstream.
: Disposition specified in the file is displayed in the browser or download () whose value can only be inline and attachment, the default is the attachment).
: Status returned to the browser to the specified response status code, the default is "200 ok"