简体   繁体   English

偏轨​​或助手

[英]rails partial or helper

I'm programming a large application using Rails 3 and I keep creating search forms like so: 我正在使用Rails 3编写大型应用程序,并且一直这样创建搜索表单:

= 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? 应该将其放入Helper方法还是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? 这会创建一个带有按钮的表单,我在这里正确吗?

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' . 如果要在应用程序的不同部分之间共享部分内容,可以将它们存储在一个名为“ shared”(或您喜欢的名称)的文件夹中,然后通过调用render :partial => '/shared/name_of_the_partial'将它们插入视图中render :partial => '/shared/name_of_the_partial'

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. 我认为,partial是完成该任务的更灵活的方法,因为它更容易更新代码,并且因为应用程序帮助程序包含在所有帮助程序中,因此您应该节省内存。

You can create that partial on shared , naming it _search.html.erb . 您可以在共享上创建该部分,并将其命名为_search.html.erb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM