You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rewrite field guide with realistic job posting form
Replace the contrived SignupForm kitchen-sink example with a
coherent JobPostingForm that demonstrates every field type in a
realistic Rails context. Use HTML5 convenience methods (.email,
.file) instead of raw input(type:). Add Rails vs Superform
side-by-side comparison showing how much ceremony Rails requires
for checkbox groups.
Superform tries to strike a balance between "being as close to HTML forms as possible" and not requiring a lot of boilerplate to create forms. This example is contrived, but it shows all the different ways you can render a form.
317
-
318
-
In practice, many of the calls below you'd put inside of a method. This cuts down on the number of `render` calls in your HTML code and further reduces boilerplate.
316
+
This example builds a realistic job posting form that demonstrates every field type Superform supports. In practice you'd extract helpers to cut down on `render` calls, but this keeps things explicit so you can see exactly what's happening.
319
317
320
318
```ruby
321
-
# Everything below is intentionally verbose!
322
-
classSignupForm < Components::Form
319
+
classJobPostingForm < Components::Form
323
320
defview_template
324
-
#The most basic type of input, which will be autofocused.
325
-
Field(:name).input.focus
321
+
#Text input, autofocused.
322
+
Field(:title).input.focus
326
323
327
-
#Input field with a lot more options on it.
328
-
Field(:email).input(type::email, placeholder:"We will sell this to third parties", required:true)
If you want to add file upload fields to your form you will need to initialize your form with the `enctype` attribute set to `multipart/form-data` as shown in the following example code:
451
-
452
-
```ruby
453
-
classUser::ImageForm < Components::Form
454
-
defview_template
455
-
# render label
456
-
Field(:image).label { "Choose file" }
457
-
# render file input with accept attribute for png and jpeg images
Render it with `<%= render JobPostingForm.new(@job_posting) %>`. For file uploads, pass `enctype: "multipart/form-data"` to the form constructor.
466
434
467
435
468
436
## Extending Superforms
@@ -557,6 +525,40 @@ Rails ships with a lot of great options to make forms. Many of these inspired Su
557
525
558
526
Rails form helpers have lasted for almost 20 years and are super solid, but things get tricky when your application starts to take on different styles of forms. To manage it all you have to cobble together helper methods, partials, and templates. Additionally, the structure of the form then has to be expressed to the controller as strong params, forcing you to repeat yourself.
559
527
528
+
Here's a checkbox group in Rails vs Superform. In Rails you need to manually wire up the field name with `[]`, track checked state against the model, generate unique ids, and point each label's `for` attribute at the right input:
529
+
530
+
```erb
531
+
<%# Rails: checkbox group for a has_many :benefits association %>
field(:benefit_ids).checkboxes(Benefit.select(:id, :name)).each do |choice|
551
+
render choice.label {
552
+
render choice.checkbox
553
+
whitespace
554
+
plain choice.text
555
+
}
556
+
end
557
+
end
558
+
```
559
+
560
+
Superform handles the field name (`benefit_ids[]`), checked state, unique ids, and label targeting automatically. The same pattern works for radio groups with `radios(...)`.
561
+
560
562
With Superform, you build the entire form with Ruby code, so you avoid the Erb gymnastics and helper method soup that it takes in Rails to scale up forms in an organization.
0 commit comments