Using ERB Partials in Sinatra
In my apprenticeship app I use almost the exact same HTML form for creating a new task and editing an existing one. Sharing a partial between the task and edit insures that any changes I want (or need) to make to my form only needs to be done once.
If you Google around for using Rails-like partials in a Sinatra app you’ll find all sorts of hand-rolled helpers and methods. Fortunately, Sinatra offers built-in support for partials, but it’s lightly documented in the Sinatra readme. Here’s a quick rundown of how I got mine working.
In my task#new and task#edit views I have the following code:
#new view
#edit view
<form method="post" action="/tasks/<%= @task.id %>">
<input type="hidden" name="_method" value="put">
<%= erb 'tasks/_form'.to_sym %>
</form>
And then my directory structure just looks like this: