简体   繁体   中英

rails partial or helper

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?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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