gpt4 book ai didi

ruby-on-rails - 渲染 'contribute' 是否与渲染 :action => 'contribute' 同义

转载 作者:行者123 更新时间:2023-12-04 06:19:18 26 4
gpt4 key购买 nike

这两种形式的“渲染”是否具有相同的效果?

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

最佳答案

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

Here's the API docs for the render function

如果我们向下滚动并单击“显示源代码”,我们可以看到它在幕后所做的事情。

注意从第 872 行开始的 block :

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

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

    关于ruby-on-rails - 渲染 'contribute' 是否与渲染 :action => 'contribute' 同义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966939/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com