params的理解和作用:
http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require
Allows you to choose which attributes should be whitelisted for mass updating and thus prevent accidentally exposing that which shouldn't be exposed. Provides two methods for this purpose: require and permit. The former is used to mark parameters as required. The latter is used to set the parameter as permitted and limit which attributes should be allowed for mass updating.
创建新的对象或更新对象属性时,进行白名单审查,把允许的属性放入白名单。
Active Record Associations
学习了belongs_to: 的细节
- 包括5个方法,
- options,
- 用lambda表达式设定想要关联的范围。You can use any of the standard querying methods inside the scope block.
http://api.rubyonrails.org/
Active Model Errors
Provides a modified Hash
that you can include in your object for handling error messages and interacting with Action View helpers
给Person的实例对象增加一个实例变量(errors对象),errors对象用来存储和使用定义(add)的错误信息。
class Person
def initialize
@errors = ActiveModel::Errors.new(self)
end
attr_reader :errors
def validate!
errors.add(:name, :blank, message: "cannot be nil") if name.nil?
end
attribute
should be set to
:base
if the error is not directly associated with a single attribute.如果不是单个的属性设置一个弹出错误,就用:base替代。
ActionDispatch::Routing::PolymorphicRoutes
多态的路径
Polymorphic URL helpers are methods for smart resolution to a named route call when given an Active Record model instance. They are to be used in combination with ActionController::Resources.
Polymorphic URL helpers are used in a number of places throughout the Rails framework:
url_for
, so you can use it with a record as the argument, e.g.url_for(@article)
;-
redirect_to
(which, in fact, usesurl_for
) so you can writeredirect_to(post)
in your controllers; - ActionView::Helpers::FormHelper uses
polymorphic_path
, so you can writeform_for(@article)
without having to specify:url
parameter for the form action;
Prefixed polymorphic helpers 前缀的多态方法
In addition to polymorphic_url
and polymorphic_path
methods, a number of prefixed helpers are available as a shorthand to action(控制器动作): "..."
in options. Those are:
-
edit_polymorphic_url
,edit_polymorphic_path
-
new_polymorphic_url
,new_polymorphic_path
Example usage:
edit_polymorphic_path(@post)
# => "/posts/1/edit"
polymorphic_path(@post, format: :pdf)
# => "/posts/1.pdf"
Functionality 函数功能
# a Comment record
polymorphic_url(record) # same as comment_url(record)
# it recognizes new records and maps to the collection
record = Comment.new
polymorphic_url(record) # same as comments_url()
# the class of a record will also map to the collection
polymorphic_url(Comment) # same as comments_url()
ActionDispatch::Routing::UrlFor
In config/routes.rb
you define URL-to-controller mappings, but the reverse is also possible: a URL can be generated from one of your routing definitions. URL generation functionality is centralized in this module.
URL从参数中生成:里面用到了UrlFor模块中的url_for()方法。
<%= link_to('Click here', controller: 'users',
action: 'new', message: 'Welcome!') %>
# => <a href="/users/new?message=Welcome%21">Click here</a>
URL从具名的路径中生成:
先在routes.rb中,生成具名的路径,resources :users
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
def base_uri
user_path(self)
end
end
User.find(1).base_uri # => "/users/1"
路由战争:product_path vs product_url
- product_url得到完整URL,所以重定向用。如redirect_to,从一个域名到另一个域名。
- product_path得到product/1的部分,用于生成链接link_to,或指定表单的动作。