Rails 部分或助手
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以我的拙见,将其放入助手中并不是一个好的做法,因为助手应该从视图中取出代码,而不是摘录视图——这就是部分视图的情况。
我肯定会使用部分来实现这个功能!
如果您想在应用程序的不同部分之间共享部分内容,可以将它们存储在名为“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'
.我认为部分是完成该任务的更易于操作的方法,因为它更容易更新代码,而且还因为您应该节省内存,因为应用程序帮助程序包含在所有帮助程序中。
您可以在共享上创建该部分,并将其命名为_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.