When I first saw helpers in the Todo list tutorial
http://manuals.rubyonrails.com/read/chapter/38#page113 I thought this is an
ugly solution
def display_items(ary)
result_string = ""
ary.each do |@item|
result_string << check_box("item", "done",
"onclick" => "
document.location.href=''/todo/toggle_check/#{@item.id}''")
+ " "
result_string << @item.description + " "
result_string << link_to("Edit", :action => "edit",
:id =>
@item.id<http://item.id>)
+ " "
result_string << link_to("Destroy",
{ :action => "destroy", :id => @item.id <http://item.id>
},
:confirm => "Are you sure you want to delete this entry:
#{@item.description
}")
result_string << "<BR />"
end
return result_string
end
Having all the ''result_string = '' and the ''+"
"'' is not very appealing.
- - - - - - -
In the very next section of the same Todo list tutorial
http://manuals.rubyonrails.com/read/chapter/38#page114 the solutions using
partials is much more appealing. It looks just like a template.
<%= check_box("item", "done", "onclick" =>
"
document.location.href=''/todo/toggle_check/#{@item.id}''")
%>
<%= @item.description %>
<%= link_to("Edit", :action => "edit", :id =>
@item.id <http://item.id>) %>
<%= link_to("Destroy", { :action => "destroy", :id
=> @item.id<http://item.id>},
:confirm => "Are you sure you want to delete this entry:
#{@item.description}")
%>
<br>
- - - - - - - -
I don''t think I would include things like check_box() or link_to() in a
helper. I would use a helper for something very small like the format
dollars helper in the Rails book p98.
module ApplicationHelper
def fmt_dollars(amt)
sprintf("$%0.2f",amt)
end
end
In this helper there is no appending to a string and no other helpers. It
just does a tiny task. The simple call in the template
<%fmt_dollars(%product.price) %> also indicates this is a method as
opposed to
a big chunk of partial template being inserted <%= render_partial
"display",
@item %>.
- Peter
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails