【问题标题】:Is render 'contribute' synonymous with render :action => 'contribute'render 'contribute' 是 render 的同义词吗:action => 'contribute'
【发布时间】:2010-11-01 07:16:37
【问题描述】:

这两种形式的“渲染”效果一样吗?

render 'contribute'
render :action => 'contribute'

【问题讨论】:

    标签: ruby-on-rails rendering


    【解决方案1】:

    简而言之:是的,它们是相同的。 但是,有时传递字符串会导致调用render :filerender :template

    Here's the API docs for the render function

    如果我们向下滚动并点击“显示源代码”,我们可以看到它在后台做了什么。

    注意从第 872 行开始的块:

    872:         elsif options.is_a?(String) || options.is_a?(Symbol)
    873:           case options.to_s.index('/')
    874:           when 0
    875:             extra_options[:file] = options
    876:           when nil
    877:             extra_options[:action] = options
    878:           else
    879:             extra_options[:template] = options
    880:           end
    

    通过查看这段代码,我们可以确定它正在尝试变得聪明。

    • 如果字符串以/ 开头(when 0 的情况),那么它将调用render :file
    • 如果字符串根本不包含/when nil 的情况),那么它将调用render :action
    • 如果字符串在字符串的中间或结尾处包含/(然后是else 大小写),那么它将调用render :template

    希望这能令人满意地回答您的问题 :-)

    【讨论】:

    • 这就是答案。谢谢。
    猜你喜欢
    • 2017-06-19
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2013-09-24
    • 2013-04-20
    • 2022-12-08
    相关资源
    最近更新 更多