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

3-29 params的理解; Active Model Errors; PolymorphicRoutes 多态的路径; ::Routing::UrlFor的更多相关文章

  1. 12月21日 简单理解Active Recore Callback, destroy_all和delete_all的区别。豆知识(alias),语言学习法(4核心)

    destroy_all and delete_all Destroy the records by instantiating each record and calling its #destroy ...

  2. java提高篇之理解java的三大特性——多态

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...

  3. 深入理解OOP(四): 多态和继承(抽象类)

    在本文中,我们讨论OOP中的热点之一:抽象类.抽象类在各个编程语言中概念是一致的,但是C#稍微有些不一样.本文中我们会通过代码来实现抽象类,并一一进行解析. 深入理解OOP(一):多态和继承(初期绑定 ...

  4. 【转】java提高篇之理解java的三大特性——多态

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...

  5. 理解oo:继承、多态、重写、重载、接口、抽象类

    1. 继承: 从多个子类中抽象出实例变量以及方法,形成更抽象的父类,避免在子类中的代码重复,维护起来更加方便.检查是否可以使用继承技术的方法是:IS A 对于类A继承自类B,类C继承自类A,那么类C和 ...

  6. 理解Spring MVC Model Attribute和Session Attribute

    作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...

  7. 【译】理解Spring MVC Model Attribute 和 Session Attribute

    作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...

  8. Rails-Treasure chest3 嵌套表单; Ransack(3900✨)用于模糊查询, ranked-model(800🌟)自订列表顺序; PaperTrail(5000✨)跟踪model's data,auditing and versioning.

    自订列表顺序, gem 'ranked-model' 多步骤表单 显示资料验证错误讯息 资料筛选和搜寻, gem 'ransack' (3900✨); 软删除和版本控制 数据汇出(csv), 自订列表 ...

  9. backbone库学习-model

    backbone库的结构: http://www.cnblogs.com/nuysoft/archive/2012/03/19/2404274.html 本文所有例子来自于http://blog.cs ...

随机推荐

  1. 《算法C语言实现》————三道题目

    1.对于N = 10,100和1000,记录你的运行环境中分别运行一下程序所花费的时间.(用python) import datetime global a a = 0 def time_1(s): ...

  2. python 循环队列的实现

    最近在做一个东西的时候发现需要用到循环队列,实现先进先出(FIFO),不断往里面添加数据,当达到某个限定值时,最先进去的出去,然后再添加.之后需要对队列里面的内容进行一个筛选,作其他处理.首先我想到了 ...

  3. corejDay1

    1.内部类: 有什么用? 1.可以访问该类定义所在作用域中的数据,包括私有数据. 2.当想定义一个回调函数而不想编写大量代码时,使用匿名内部类比较便捷. 3.内部类可以对同一个包中的其他类隐藏起来. ...

  4. kafka-python的API简单介绍

    在上一篇文章中说明了kafka-python的API使用的理论概念,这篇文章来说明API的实际使用. 在官方文档详细列出了kafka-python的API接口https://kafka-python. ...

  5. mysql5.7密码设置

    mysql5.7版本引入了强制更改密码的举措,只能吐槽一句,shit!mysql5.7安装安装完mysql之后,mysql已经随机指定了一个初始化密码,可以在mysql的错误日志中找到初始化密码: c ...

  6. c++ 11和java 8都支持lambda表达式

    c++ 11居然都支持lambda表达式了,看了这确实是有必要了. 具体可见http://www.cprogramming.com/c++11/c++11-lambda-closures.html

  7. wireshark捕获表达式之Berkeley Packet Filter (BPF) syntax

    就网络抓包来说,绝大部分的情况下,我们都是对特定的ip/端口/协议进行捕获和分析,否则就会有大量的垃圾报文,使得分析和性能低下.大部分的抓包工具都采用BPF语法,具体可参考 http://biot.c ...

  8. 08: python基础练习题

    1.while循环实现输出2 - 3 + 4 - 5 + 6 ... + 100 的和 # 使用while循环实现输出2 - 3 + 4 - 5 + 6 ... + 100 的和 s = 0 i = ...

  9. php 设置模式 单元素模式(单例模式或单件模式)

    单元素模式: 某些应用程序资源是独占的,因为有且只有一个此类型的资源.应用程序每次包含且仅包含一个对象,那么这个对象就是一个单元素.指的是在应用程序的范围内只对指定的类创建一个实例.通常该模式中包含一 ...

  10. 20145105 《Java程序设计》第9周学习总结

    20145105 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 一.JDBC入门 (一)JDBC简介 厂商在操作JDBC驱动程序时,依操作方式可将驱动程序分为 ...