Rails way --->< The Rails Way> excerpt (2) the use of the controller

2010-05-27  来源:本站原创  分类:Ruby  人气:215 

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.
exp:

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.

Quote

**: 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.
(1): json
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

(2): xml
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

Quote

(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

Quote

Common status codes
307 temporary redirect (request for a temporary transfer of resources to another URL)
401 authorized
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)

before_filter :require_authentication

Notes:
(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
skip_before_filter: authenticate

(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

Quote

: 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"

相关文章
  • Rails way --->< The Rails Way> extract (1) Rails environment and configuration 2010-04-28

    1. Every time you start a process (such as Webrick server) when dealing with Rails request, the first thing that occurred loading config / enviroment.rb For example publich / dispatch.rb top of the file require File.dirname(_FILE_) + "/../config/envi

  • Rails way --->< The Rails Way> extract (3) routing 2010-05-27

    1. Routing system is a powerful way to send complex, it accepts URL, proper addressing, routing rules can also be used as a template generated URL string. (Accept the URL belongs to the basic operations, building URL count advanced to take advantage

  • Rails 2.3 + Rails 3 + Authlogic + I18n 2010-09-23

    If you are using Rails 2.3 Rails 3 Authlogic I18n error wrong number of arguments (4 for 3) (ArgumentError), in consequence of the fact that Rails 3 require I18n> = 0.3.6 version, and Authlogic works with earlier versions of I18n. One solution to pre

  • [Translated] from the Rails Envy the Rails Cache tutorial part1 2010-10-22

    http://railsenvy.com/2007/2/28/rails-caching-tutorial The tutorial has two parts, the first part of this is about page cache, second about the other cache. This article is mainly about the basis of fact, but combines a number of examples, and accompa

  • 在Ruby on Rails中使用Rails Active Resource的教程 2015-03-04

    这篇文章主要介绍了在Ruby on Rails中使用Rails Active Resource的教程,本文来自于IBM官方网站技术文档,需要的朋友可以参考下 简介 当今的应用程序不仅需要和基于浏览器的客户端互操作,还需要和其他应用程序互操作.为实现互操作性,web 应用程序通常提供一个 web 服务 API.web 服务 API 通过一个网络(比如 Internet)提供对应用程序 的远程访问.直到最近,web 服务 API 还使用重型.复杂的基于 SOAP 的 web 服务集成,这种 web

  • Rails way --->< The Rails Way> excerpt (2) the use of the controller 2010-05-27

    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 y

  • Rails way --->< The Rails Way> excerpt (6) using ActiveRecord 2010-06-01

    1. When you Zai Xia Run Rails development mode server O'clock, Web browsing device right through the data Biao Li Ji Jie Gou's change will come out in the ActiveRecord object Zhong Fan Ying. However, if you run Zhao Rails of Kong Zhitai, Name changes

  • Rails way --->< The Rails Way> extract (2) the use of the controller 2010-05-27

    1. Your controller is only used to map URL (including some other HTTP requests), and view your data model and template cascaded together, the results of their feedback to the HTTP response. 2. In practice, very important thing to remember about your

  • Rails way --->< The Rails Way> extract (4) REST, Resources, and Rails 2010-05-31

    1.REST (Representational State Transfer) have the appearance of state transition is one of the main reasons for using REST for all Web developers should think about how to name and organization in the application of resources and action. 2. Using Rai

  • Rails way --->< The Rails Way> extract (6) using ActiveRecord 2010-06-01

    1. When you Zai Xia Run Rails development mode server O'clock, Web browsing device right through the data Biao Li Ji Jie Gou's change will come out in the ActiveRecord object Zhong Fan Ying. However, if you run Zhao Rails of Kong Zhitai, Name changes

  • Rails way --->< The Rails Way> extract (six) using ActiveRecord 2010-06-01

    1. When you Zai Xia Run Rails development mode server O'clock, Web browsing device right through the data Biao Li Ji Jie Gou's change will come out in the ActiveRecord object Zhong Fan Ying. However, if you run Zhao Rails of Kong Zhitai, Name changes

  • [Translated] from the Rails Envy the Rails Cache tutorial part2 2010-10-22

    Original Address: http://railsenvy.com/2007/3/20/r ... ING-TUTORIAL-Part-2 Part1 Address: http://www.ruby-lang.org.cn/forums/thread-3158-1-1.html Order the preparation of this tutorial is based on the efficiency of each sort of cache, Page caching th

  • Rails综合报道:Rails 2.2将会是线程安全的,Rails Edge支持ETags 2014-01-27

    David Heinemeier Hansson 最近提到 Rails 2.2 将会修补一个 Rails 存在已久的问题:其缺乏线程安全性.Charles Nutter 在文章中进一步阐述了线程安全的 Rails 意味着什么: 本质上,其意味着去掉每个输入的请求所添加的较为粗糙的锁,取而代之的是为需要在线程间共享的资源加上更为精细的锁.采用的方法如,日志子系统中的数据结 构要么修改成不在线程间共享,要么需要设计恰当的锁机制以保证两个线程之间不会发生交互,不会影响彼此的数据结构或发生冲突.对于一个

  • Rails Cells:让Rails支持面向组件的开发 2014-08-09

    Rails Cells的目标是"把面向组建开发的优势带进Ruby on Rails网络应用程序平台".单元(cell)就像是一个轻量级的控制器以及与其相关的可以嵌入网站的视图(类似于Smalltalk的网络应用程序框架Seaside). 那么Rails现在的功能有什么问题呢?Mike Pence的博客里是这么说的: 很多Rails项目通常用before_filters和子视图(partials)来填补空缺,但是这种方法本身有些问题.共享的子视图有许多初始化代码,这使applicatio

  • In the Linux platform to install and configure Ruby on Rails Detailed 2009-03-02

    ruby on rails recommend the production environment is running Linux / FreeBSD / Unix, or Unix family of operating systems, using lighttpd + FCGI solution. The following will be my Linux operating system, lighttpd + FCGI, MySQL database as an example,

  • rails version of the change (b) - rails of the introduction 2009-03-09

    Prompted the new Rails 2.2 features October 24, Rails has released a preview version 2.2, Rails This is the history of a major version upgrade! Mainly includes the following major changes: 1, support internationalized! Rails2.2 integrates a i18n fram

  • rails version of the change (1) - rails of the introduction 2009-03-09

    Rails framework are at the first time in July 2004, its R & D is the 26-year-old Danes David Heinemeier Hansson. Different from complex Web development framework, Rails is more in line with actual needs and more efficient Web development framework. R

  • Ruby on Rails installation notes 2009-03-11

    First, install the ruby, the first from the http://www.ruby-lang.org/zh_CN/downloads/ Ruby download the Windows version of the stability of the installation Step in the installation of Ruby 1.8.6 (md5: 00540689d1039964bc8d844b2b0c7db6) stable version

  • At Debian (includes Ubuntu) use the APT platform installed Ruby and Rails 2009-03-11

    Passenger Responsible person Phusion , Announced two days before the DebGem (RubyGem to APT conversion service) After the Debian and Ubuntu systems administrators and users can use APT to install and maintain ways to ruby and rails systems environmen

  • 09 economic situation may stimulate the Ruby on Rails? 2009-03-11

    It is weeks, when the financial turmoil hit all over the sky at the time, layoffs crisis become the most reluctant to face all this. However, it will never be able to evade the truth. Earlier, PPLive and six houses several IT companies layoff news ha