1.Getting Started with Rails
1.1 数据在MVC之间共享传递的问题
1.2
2. Action Controller Overview
2.1 Parameters
There are two kinds of parameters possible in a web application. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the params hash in your controller.
2.2 Hash and Array Parameters
2.3 ? Routing Parameters
2.4 ?default_url_options
2.2 Session
3. Layouts and Rendering in Rails
3.1 Creating Responses
From the controller’s point of view, there are three ways to create an HTTP response:
- Call render to create a full response to send back to the browser
- Call redirect_to to send an HTTP redirect status code to the browser
- Call head to create a response consisting solely of HTTP headers to send back to the browser
3.1.1 Rendering by Default: Convention Over Configuration in Action
The rule is that if you do not explicitly render something by the end of the controller action, rails will look for the action_name.html.erb template in the controllers view path and then render that, so in this case, Rails will render the app/views/books/index.html.erb file. 如果controller没有涉及任何关于render的说明,那么使用默认规则:action_name.html.erb
比如这种情况:
class
BooksController < ApplicationController
def
index
@books
= Book.all
end
end
def
index
@posts
= Post.all
respond_to
do
|format|
format.html
# index.html.erb
format.xml { render
:xml
=>
@posts
}
end
end
3.1.2 Using render
In most cases, the ActionController::Base#render method does the heavy lifting of rendering your application’s content for use by a browser. There are a variety of ways to customise the behaviour of render . You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON , or XML . You can specify the content type or HTTP status of the rendered response as well.
render_to_string method takes exactly the same options as render , but it returns a string instead of sending a response back to the browser.
(1)Rendering Nothing:render
:nothing
=>
true
(2)Rendering an Action’s View
render "edit"
render :edit
render :action => "edit" (:action option is no longer necessary in Rails 3.0)
Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render . 这句说得好,这里:action 的作用是为了指明对应那个view,而不是去执行controller中执行这个action方法的。
比如下面程序,:action 只是为了说明 that If the call to update_attributes fails, calling the update action in this controller will render the edit.html.erb template belonging to the same controller .
def
update
@book
= Book.find(params[
:id
])
if
@book
.update_attributes(params[
:book
])
redirect_to(
@book
)
else
render
:action
=>
"edit"
end
end
end
render
'products/show' or
render
:template
=>
'products/show' (Rails 2.2)
render
:file
=>
"/u/apps/warehouse_app/current/app/views/products/show" (Rails2.2)
redirect_to photos_path
redirect_to
:back
def
index
@books
= Book.all
end
def
show
@book
= Book.find_by_id(params[
:id
])
if
@book
.
nil
?
render
:action
=>
"index"
end
end
def
index
@books
= Book.all
end
def
show
@book
= Book.find_by_id(params[
:id
])
if
@book
.
nil
?
redirect_to
:action
=>
:index
end
end
def
index
@books
= Book.all
end
def
show
@book
= Book.find_by_id(params[
:id
])
if
@book
.
nil
?
@books
= Book.all
render
"index"
,
:alert
=>
'Your book was not found!'
end
end
HTTP/1.1 400 Bad Request.
When Rails renders a view as a response, it does so by combining the view with the current layout. Within a layout, you have access to three tools for combining different bits of output to form the overall response:
- Asset tags
- yield and content_for
- Partials
- auto_discovery_link_tag
- javascript_include_tag
- stylesheet_link_tag
- image_tag
- video_tag
- audio_tag
<%=
render
"link_area"
,
:layout
=>
"graybar"
%>
<%=
render
:partial
=>
"product"
,
:collection
=>
@products
%>