Rails 部分或助手

发布于 2024-10-27 16:50:58 字数 515 浏览 6 评论 0原文

我正在使用 Rails 3 编写一个大型应用程序,并且不断创建搜索表单,如下所示:

= form_tag search_companies_path, :method => "get" do
    = label_tag :search
    = text_field_tag :search
    = submit_tag "Search"

应该将其放入 Helper 方法还是 Partial 中?

我试图通过助手让它工作:

module ApplicationHelper
  def search_form(path)
    form_tag path, :method => "get" do
      label_tag :search
      text_field_tag :search
      submit_tag "Search"
    end
  end
end

这会创建一个带有按钮的表单,我在这里走对了吗?

I'm programming a large application using Rails 3 and I keep creating search forms like so:

= form_tag search_companies_path, :method => "get" do
    = label_tag :search
    = text_field_tag :search
    = submit_tag "Search"

Should this be put into a Helper method or Partial?

I tried to get it working through a Helper:

module ApplicationHelper
  def search_form(path)
    form_tag path, :method => "get" do
      label_tag :search
      text_field_tag :search
      submit_tag "Search"
    end
  end
end

This creates a form with a button, am I on the right track here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

毁虫ゝ 2024-11-03 16:50:58

以我的拙见,将其放入助手中并不是一个好的做法,因为助手应该从视图中取出代码,而不是摘录视图——这就是部分视图的情况。

我肯定会使用部分来实现这个功能!

如果您想在应用程序的不同部分之间共享部分内容,可以将它们存储在名为“shared”(或您喜欢的任何名称)的文件夹中,然后通过调用 render :partial => 将它们插入视图中。 '/shared/name_of_the_partial'。

Putting it into a helper - in my humble opinion - is not a good practice since helpers are supposed to take code out of views, not to take views excerpts - which is the case for partials.

I would definitely use partials for this function!

If you want to share a partial between different parts of your application, you can store them in a folder called "shared" (or whatever name you like) and insert them into the view by calling render :partial => '/shared/name_of_the_partial'.

梦忆晨望 2024-11-03 16:50:58

我认为部分是完成该任务的更易于操作的方法,因为它更容易更新代码,而且还因为您应该节省内存,因为应用程序帮助程序包含在所有帮助程序中。

您可以在共享上创建该部分,并将其命名为_search.html.erb

i think that partial is a more manutenible way to accomplish that task, because it's easier updating the code and also because you should save memory because the application helper is include in all helper.

You can create that partial on shared, naming it _search.html.erb.

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