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

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

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

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

1
<%= render @products %>

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

action_view/renderer/partial_renderer (Rails 4.2)

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

打开 rails console 可以试试

1
2
[1] pry(main)> User.new.to_partial_path
=> "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. html通配符

    ♠ ♠ ♠ 黑桃 ♣ ♣ ♣ 梅花 ♥ ♥ ♥ 红桃,心 ♦ ♦ ♦ 方块牌 ◊ ◊ ◊ 菱形 † † † 匕首 ‡ ‡ ‡ 双剑号 ¡ ¡ ¡ 反向感叹号 ¿ ¿ ¿ 反向问号 ← ← ← 左箭头 ...

  2. Nginx负载均衡(转发)

    http://www.cnblogs.com/jalja/p/6117881.html 一.反向代理 正向代理: 客户端要获取的资源就在服务器上,客户端请求的资源路径就是最终响应资源的服务器路径,这就 ...

  3. 解决fixed布局里内容不滚动问题

    //注意给父级加上 (1)横向滚动 left:0; right:0; 然后想要横向滚动的话 overflow-x:scroll; overflow-y:hidden; (2)竖直方向滚动top:0;b ...

  4. PAT Advanced 1003 Emergency (25) [Dijkstra算法]

    题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...

  5. springboot-jar-web

    预览 与springboot-jar的区别是: 1.pom.xml 将 <dependency> <groupId>org.springframework.boot</g ...

  6. python学习---format、当前时间

    1.数字格式化   format <  :左对齐 >  :右对齐 a = “随机数是{:>4d}”.format(1)       结果是0001 2.当前时间 import dat ...

  7. Vue.js(15)之 json-server搭建模拟的API服务器

    json-server搭建模拟的API服务器 运行命令 npm install json-server -D 全局安装 json-server 项目根目录下创建 mock 文件夹 mock 文件夹下添 ...

  8. Unity3D一些基本的概念和一些基本操作

    场景:整个游戏由场景组成,一个游戏至少要有一个场景,如果把所有的游戏画面放在一个场景里也是可以的,如果游戏非常非常的大,如果所有的东西都放到一个场景里那么结构就不是那么清晰了而且处理起来就会麻烦一些, ...

  9. C++ spdlog日志管理

    [1]spdlog简介 spdlog是一个开源的.快速的.仅有头文件的基于C++11实现的一款C++专用日志管理库. [2]源码下载 下载地址:https://github.com/gabime/sp ...

  10. JaveSE--getResource

    System.out.println(ConfigUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath()); ...