都知道的, 在 Rails 的 View 里边渲染集合的时候, 会用到 render 方法参数的 collection 选项

  1. 1
  1. <%= render partial: "product", collection: @products %>

而不是遍历集合来渲染单个模板.

渲染集合还有个简写形式. 假设 @productsproduct 实例集合, 在 index.html.erb中可以直接写成下面的形式, 得到的结果是一样的:

  1. 1
  1. <%= render @products %>

这里, Rails 做的魔法其实是去找遍历成员的 to_partial_path

action_view/renderer/partial_renderer (Rails 4.2)

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. def partial_path(object = @object)
  2. object = object.to_model if object.大专栏  Rails render collection 的魔法"n">respond_to?(:to_model)
  3. path = if object.respond_to?(:to_partial_path)
  4. object.to_partial_path
  5. else
  6. raise ArgumentError.new("'#{object.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.")
  7. end
  8. if @view.prefix_partial_path_with_controller_namespace
  9. prefixed_partial_names[path] ||= merge_prefix_into_object_path(@context_prefix, path.dup)
  10. else
  11. path
  12. end
  13. end

打开 rails console 可以试试

  1. 1
  2. 2
  1. [1] pry(main)> User.new.to_partial_path
  2. => "users/user"

这里也可以把 user 这个 model 的 to_partial_path 重写, 返回表示渲染路径的字符串,

如果你的某个 PORO 实现了 to_partial_path, 那对应的 collection 也可以直接用类似的方式去 render

Rails render collection 的魔法的更多相关文章

  1. rails render

    Render結果 在根據request資訊做好資料處理之後,我們接下來就要回傳結果給用戶.事實上,就算你什麼都不處理,Action方法裡面空空如也,甚至不定義Action,Rails預設也還是會執行r ...

  2. rails member collection

    resources :theses do resources :document do get :download, :on => :member end end <a class=&qu ...

  3. rails 杂记 - erb 中的 link_to 的 ActiveRecord 与 render 中的 partial

    路由及路由参数 <%= link_to 'My Blog', {controller: 'articles', demo: "lidsi"}, class: "bl ...

  4. ASP.NET MVC与RAILS3的比较

    进入后Web年代之后,MVC框架进入了快速演化的时代,Struts等垂垂老矣的老一代MVC框架因为开发效率低下而逐渐被抛弃,新一代的MVC则高举敏捷的大旗,逐渐占领市场,其中的代表有Rails (ru ...

  5. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

  6. ruby on rails 中render的使用

    最近写ror,因为比较菜,很多东西不知道,只能看一点查一点了 render 先上点搜集的常用方式 render :action => "long_goal", :layout ...

  7. rails 杂记 - render and layout

    官方文档:http://guides.rubyonrails.org/layouts_and_rendering.html 渲染 view 渲染 html.rb 与相应的 action control ...

  8. rails局部模板 render

    <%= render partial: 'file' %> file是以_开头命名的文件,比如_cart.html.erb 这样就可以用render来调用了 还可以传参数 比如 rails ...

  9. ruby on rails 中render的

    Ruby rails页面跳转代码如下: 1.render(:text => string) 2.render(:inline => string, [:type => "r ...

随机推荐

  1. Vulkan 开发学习资料汇总

    开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...

  2. C++程序--helloworld

    #include<iostream>using namespace std; int main(){ cout << "hello world!" < ...

  3. Mybatis框架的简单配置

    Mybatis 的配置 1.创建项目(当然,这是废话) 2.导包 下载mybatis-3.2.0版:https://repo1.maven.org/maven2/org/mybatis/mybatis ...

  4. 自己组装一台1U服务器

    视频资料链接 自己组装一台1U服务器 执行思路: 1.评估访问了,根据需求确定服务器要求 2.根据要求选择硬件:冗余.稳定等 3.搜索主流服务器参数进行对比,及对比价格 4.咨询IDC机房价格 DIY ...

  5. CTF -攻防世界-web新手区

    直接f12出来 先用get后加/?a=1 然后火狐装hackbar(老版本)f12 post b=2 搞定 Hackbar:https://github.com/Mr-xn/hackbar2.1.3 ...

  6. JAVAEE 和项目开发(第一课:浏览器和服务器的交互模式和HTTP协议的概念和介绍)

    互联网的发展非常迅速,但是万变不离其宗.学习 web 开发,需要我们对互 联的交互机制有一定的了解.为了更好的理解并掌握 Servlet,在正式学习 Servlet之前需要对 web 开发中客户端和服 ...

  7. 开源PLM软件Aras详解八 Aras之RelationshipTypes关系类详解

    在Aras中,在之前ItemType解析中有提到,Aras中实际ItemType对应的就是一张表,那么,ItemType与ItemType之间是如何关联的呢, 如果我们需要捋清楚ItemType与It ...

  8. 对CI框架中几个文件libraries

    对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知    时间:2014-10-20 11:37   阅读数:117   作者:xbdadmin [导读] 1.lib ...

  9. Android如何用一个TextView显示不同颜色得字符

    最近做一个项目,需要一个字符串显示不同的颜色.当时直接想到的就是用多个TextView来拼接,但是如果字符数量多的话,这样写是非常麻烦得.而且还要增加很多控件. 后来发现一个非常方便得方法.直接看代码 ...

  10. 吴裕雄--天生自然 JAVASCRIPT开发学习: this 关键字

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...