都知道的, 在 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. C语言预处理理论2

    C语言预处理理论1.头文件包含(1)#include <>和#include ""区别:<>专门用来包含系统提供的头文件(就是系统自带的,不是程序员自己写的 ...

  2. Alpha版(内部测试版发布)

    使用说明: 使用环境:android 5.0以上 使用流程: 1.注册与登陆 可以通过游客和用户两个模式登陆 用户模式:进入后会有模拟位置图,每一环代表不同的距离 底部菜单栏表示不同的功能,消息栏可以 ...

  3. Swift - 从相册中选择视频(过滤掉照片,使用UIImagePickerController)

    (本文代码已升级至Swift4) 有时我们需要从系统相册中选择视频录像,来进行编辑或者上传操作,这时使用 UIImagePickerController 就可以实现. 默认情况下,UIImagePic ...

  4. 实验吧Web-中-简单的sql注入之2

    输入union select.order by这些关键字会提示SQLi detected! 说明对于关键字有防护 输入1 union也会提示SQLi detected! 但是如果去掉空格:1union ...

  5. CPU的成本构成

    1)设计成本: 工程师的工资,EDA等开发工具的费用.设备费用.场地费用等等. 2)硬件成本: 硬件成本=(晶片成本+掩膜成本+封装.测试成本)/成品率 1.晶片成本 一片硅晶圆 晶片成本=晶圆成本/ ...

  6. DES与MD5加密

    using System; using System.Data; using System.Configuration; using System.Web; using System.Security ...

  7. org.springframework.test.context.junit4.SpringJUnit4ClassRunner

    项目中有了spring-test的依赖,里面确实也有 org.springframework.test.context.junit4.SpringJUnit4ClassRunner 此类,但是项目就是 ...

  8. k8s pod.yml解释

    apiVersion: v1kind: Podmetadata:  name: yueying  namespace: kube-public  labels:    name: testpodssp ...

  9. Java线程——线程之间的通信

    Java中多线程间的通信是怎么实现的? 线程通信的方式: (1)共享变量 线程之间的通信可以通过发送信号,发送信号的一个简单方法就是再共享的对象里面设置信号值.线程A在一个同步块中设置boolean型 ...

  10. java线程——线程局部变量

    一,线程局部变量ThreadLocal的作用 用于实现线程内部的数据共享,既对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,在另一个线程访问的时候,访问的由是另一份数据. 每个线程调用 ...