Created
September 4, 2012 16:05
-
-
Save sam/3622831 to your computer and use it in GitHub Desktop.
First Class Forms API notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # NOTE: The actual path is: views/forms/person.html.erb %> | |
<% # Or we can override the default rendering like so: %> | |
<h2>People Form</h2> | |
<% # By default the form-helper will POST to the current location %> | |
<% form do |form| %> | |
<table> | |
<tr><td>First Name:</td><td><%= form.field :first_name %></td></tr> | |
<tr><td>Last Name:</td><td><%= form.field :last_name %></td></tr> | |
<tr><td>Age:</td><td><%= form.field :age %></td></tr> | |
<tr><td>Notes:</td><td><%= form.field :notes %></td></tr> | |
<% # Each of the above returns a Form::Field object with the options | |
# for that field so it knows how to render if #to_s is called. | |
# Same for below. %> | |
<tr><td> </td><td><%= form.submit %></td></tr> | |
<% # You could also handle rendering manually: %> | |
<tr> | |
<td>Notes:</td> | |
<td> | |
<input type="text" size="100" value="<%= form[:notes] %>"/> | |
</td> | |
</tr> | |
</table> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddressBook < Harbor::Application | |
class People < Harbor::Controller | |
# Should we switch to implicit renders? | |
# I mean, the vast majority of the time | |
# you'll need to render a template here. | |
# If we don't explicitly pass the variables | |
# anymore, why do we require you to explicitly | |
# pass the template name when that could be | |
# determined for you by convention? | |
get ":name/edit" do |name| | |
@person = db.people.get_by_name(name) | |
@form = PersonForm.new(@person) | |
render "people/edit" | |
end | |
# Can the whole cycle of: | |
# valid? | |
# save and redirect | |
# else | |
# re-render | |
# | |
# ...be abstracted? | |
post do |person| | |
@form = PersonForm.new(person) | |
if @form.valid? | |
# Form would respond_to #to_hash, | |
# allowing you to pass it directly to #save | |
# and have your typecast variables set. | |
db.people.save(@form) | |
redirect "people" | |
else | |
render "people/edit" | |
end | |
end | |
end # class People < Controller | |
end # class AddressBook < Application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # NOTE: The actual path is: views/people/edit.html.erb %> | |
<% # We can render the default bureaucrat form like so now: %> | |
<%= @form %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersonForm < Harbor::Form | |
string :first_name | |
string :last_name | |
text :notes, max: 2000 | |
integer :age, min: 18 | |
end |
Noted, removed comment, changed "textarea" call to just "text".
The Form DSL used above is basically the same as Rails BTW: http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object
This was just by coincidence.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Regarding PersonForm, Bureaucrat basically already does that. It uses "string" to identify a single-line text input, and "text" to identify a textarea (see https://github.com/tizoc/bureaucrat/blob/master/examples/fields_test/config.ru)