Ruby On Rails - 我在哪里寻找渲染的源函数:partial => 'x'在一个视图中?

发布于 2024-09-19 17:36:09 字数 367 浏览 3 评论 0原文

我正在修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-09-26 17:36:09

我假设您想更改部分本身的代码,并且只想知道在哪里可以找到该部分的模板,而不是更改 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 syntax render :partial => "documents/form" is Ruby's way of passing keyword arguments to method; it is essentially just saying to render documents/form as a partial template. (In Ruby, this is actually equivalent to render({:partial => "documents/form"}), which is just invoking method render, 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 in app/views, you will probably be looking for app/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.

巷子口的你 2024-09-26 17:36:09

您正在 ActionController::Base 类中寻找 render 方法。它以选项的哈希值作为参数。 (顺便说一句,如果你在看到 ruby​​ 哈希时无法识别它,也许你“调整”rails 有点早了。你总是可以先浏览一些 Ruby 教程)

如你所知,monkeypatching 非常非常糟糕,等等等等,现在去搬起石头砸自己的脚吧:)

  class ActionController::Base
    def render
      # Now I'm in charge of rendering!
    end
  end

You're looking for render method in ActionController::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 :)

  class ActionController::Base
    def render
      # Now I'm in charge of rendering!
    end
  end
有深☉意 2024-09-26 17:36:09

了解您想要进行哪种调整可能会有所帮助。修改像 render 这样的核心函数很可能不是一个好主意,而且肯定有一种更简单的方法。

通过覆盖它,您将影响使用渲染的所有其他地方。为什么不创建自己的渲染方法作为助手呢?

# app/helpers/application_helper.rb
module ApplicationHelper
  def render_my_crazy_thing
    return "stuff you want to see: " + render(:partial => 'documents/form')
  end
end

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?

# app/helpers/application_helper.rb
module ApplicationHelper
  def render_my_crazy_thing
    return "stuff you want to see: " + render(:partial => 'documents/form')
  end
end

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文