zoukankan      html  css  js  c++  java
  • 3-29 params的理解; Active Model Errors; PolymorphicRoutes 多态的路径; ::Routing::UrlFor

    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: 的细节

    1. 包括5个方法,
    2. options,
    3. 用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
     add(attribute, message = :invalid, options = {})
    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, uses url_for) so you can write redirect_to(post) in your controllers;
    • ActionView::Helpers::FormHelper uses polymorphic_path, so you can write form_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_urledit_polymorphic_path

    • new_polymorphic_urlnew_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,或指定表单的动作。

  • 相关阅读:
    JStack分析cpu消耗过高问题
    Machine Learning in Action – PCA和SVD
    Machine Learning in Action -- FP-growth
    Machine Learning in Action -- 树回归
    Machine Learning in Action -- 回归
    Kafka 0.8 配置参数解析
    统计学习方法笔记 -- 隐马尔可夫模型
    Machine Learning in Action -- AdaBoost
    统计学习方法笔记 -- Boosting方法
    Andrew Ng机器学习公开课笔记–Reinforcement Learning and Control
  • 原文地址:https://www.cnblogs.com/chentianwei/p/8668554.html
Copyright © 2011-2022 走看看