Ruby On Rails - 我在哪里寻找渲染的源函数:partial => 'x'在一个视图中?
我正在修改 Ruby Rails 应用程序 - Redmine。我想修改一个呈现部分预览的函数。我认为这是通过
<%= render :partial => 'documents/form' %>
我需要修改附加的函数来完成的。
我可以在哪里寻找这个? render
是我正在寻找的函数吗? :partial
是参数吗? =>
运算符的作用是什么?
我确实意识到学习 Ruby 和 Rails 可以解决这些问题,但对于目前的情况,我只需要了解足够的知识即可进行一些小调整。我知道它不在控制器中。
I am modifying a Ruby Rails application - Redmine. I want to modify a function that renders a partial preview. In the view I believe this is accomplished via
<%= render :partial => 'documents/form' %>
I need to modify the function this is attached to.
Where might I look for this?
Is render
the function I am looking for?
Is :partial
a parameter? What does the =>
operator do?
I do realize learning Ruby and Rails would solve these issues, but for the current situation I just need to know enough to make a small tweak. I know it's not in the controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您想更改部分本身的代码,并且只想知道在哪里可以找到该部分的模板,而不是更改
render
方法,该方法是 Rails 本身的一部分,正如其他一些答案所建议的那样。render 方法告诉 Rails 渲染一个特定的模板,可能会传入一些参数。partial 是一种模板,旨在仅渲染页面的一个片段,例如单个小部件或页面的单个部分。语法
render :partial =>; “documents/form”
是 Ruby 将关键字参数传递给方法的方式;它本质上只是说将文档/表单渲染为部分模板。 (在 Ruby 中,这实际上相当于render({:partial => "documents/form"})
,它只是调用方法render
,传入一个哈希值表,其中:partial
(关键字)映射到"documents/form"
(字符串)。因此,实际呈现的代码是部分
documents/forms
。按照惯例,部分内容是从前缀为_
的文件中加载的;如果您使用默认的 ERb 模板格式,那么它们可能会以.html.erb
结尾。由于所有视图代码都存储在app/views
中,因此您可能会查找app/view/documents/_form.html.erb
。是的,这不是一个特别明显的约定,但这就是适合您的 Rails。有关详细信息,请参阅有关部分和渲染的 Rails 指南。
I'm assuming that you would like to change the code for the partial itself, and just want to know where to find the template for that partial, not change the
render
method, which is part of Rails itself, as some of the other answers have suggested.The
render
method tells Rails to render a particular template, possibly with some parameters passed in. A partial is a kind of template that is intended to render only a fragment of a page, such as an individual widget or a single section of the page. The syntaxrender :partial => "documents/form"
is Ruby's way of passing keyword arguments to method; it is essentially just saying to renderdocuments/form
as a partial template. (In Ruby, this is actually equivalent torender({:partial => "documents/form"})
, which is just invoking methodrender
, passing in a hash table in which:partial
, a keyword, maps to"documents/form"
, a string).So, the code that will actually be rendered is the partial
documents/forms
. By convention, partials are loaded from files prefixed with_
; and if you're using the default ERb template format, then they will likely end in.html.erb
. As all view code is stored inapp/views
, you will probably be looking forapp/view/documents/_form.html.erb
. Yes, this is a not particularly obvious bit of convention, but that's Rails for you.See the Rails Guide on partials and rendering for more information.
您正在
ActionController::Base
类中寻找render
方法。它以选项的哈希值作为参数。 (顺便说一句,如果你在看到 ruby 哈希时无法识别它,也许你“调整”rails 有点早了。你总是可以先浏览一些 Ruby 教程)如你所知,monkeypatching 非常非常糟糕,等等等等,现在去搬起石头砸自己的脚吧:)
You're looking for
render
method inActionController::Base
class. It takes hash of options as parameter. (BTW, if you can't recognize ruby hash when you see one, maybe it's a bit early for you to "tweak" rails. You can always go through some Ruby tutorial first)As you know, monkeypatching is very, very bad, blah-blah-blah, now go and shoot yourself in the foot :)
了解您想要进行哪种调整可能会有所帮助。修改像 render 这样的核心函数很可能不是一个好主意,而且肯定有一种更简单的方法。
通过覆盖它,您将影响使用渲染的所有其他地方。为什么不创建自己的渲染方法作为助手呢?
ActionView#render 方法处理所有不同的渲染情况,切换不同的参数:部分、对象集合等。 'documents/form' 参数是 hash 的 ruby 语法(key => value)。
It might help to know what kind of tweak you're trying to do. Modifying a core function like render is most likely not a good idea, and there's certainly a much easier way.
By overriding this, you'll affect every other place where render is used. Why not create your own render method as a helper?
The ActionView#render method handles all different cases of rendering, switching on different parameters: partials, collections of objects, etc. The
:partial => 'documents/form'
parameter is the ruby syntax for a hash (key => value).