Ruby July 29, 2009

Custom Block Helper in ERB

Something I learned how to do in rails, but had a hard time replicating without it, is create a custom helper that accepts a block. I figured out how to write to the screen from the helper. @_out_buf is the name of the variable I needed.

# myview.erb
<% form("/login", :post) do %>
	<p>Username: <input type="text" name="username"></p>
	<p>Password: <input type="password" name="password"></p>
<% end %>

# helpers.rb
def form(url, method, &block)
  @_out_buf << "<form action='#{url}' method='post'>"
  @_out_buf << "<input type='hidden' name='_method' value='#{method}'>"
  yield
  @_out_buf << "</form>"
end

Your comment was added successfully
Your comment could not be added

Leave a comment