使用 Phlex 组件构建高度可定制的 HTML 表单
The best Rails form library. Whether you're using ERB, HAML, or Phlex, Superform makes building forms delightful.
No more strong parameters headaches. Add a field to your form and it automatically gets permitted. Never again wonder why your new field isn't saving. Superform handles parameter security for you.
Works beautifully with ERB. Start using Superform in your existing Rails app without changing a single ERB template. All the power, zero migration pain.
Concise field helpers. Field(:publish_at).date, Field(:email).email, Field(:price).number — intuitive methods that generate the right input types with proper validation.
RESTful controller helpers Superform's save and save! methods work exactly like ActiveRecord, making controller code predictable and Rails-like.
Superform is a complete reimagining of Rails forms, built on solid Ruby foundations with modern component architecture under the hood.
Support this project and become a Superform pro by ordering the Phlex on Rails video course.
Add to the Rails application's Gemfile by executing:
$ bundle add superformThen install it.
$ rails g superform:installThis will install both Phlex Rails and Superform.
Superform works instantly in your existing Rails ERB templates. Here's what a form for a blog post might look like:
New Post
The form automatically generates proper Rails form tags, includes CSRF tokens, and handles validation errors.
Notice anything missing? Superform doesn't need `` tags around every single line, unlike all other Rails form helpers.
You probably want to use the same form for creating and editing resources. In Superform, you extract forms into their own Ruby classes right along with your views.
# app/views/posts/form.rb
class Views::Posts::Form Cool, but you're about to score a huge benefit from extracting forms into their own Ruby classes with automatic strong parameters.
Include Superform::Rails::StrongParameters in your controllers for automatic parameter handling:
class PostsController Much better!
radios and checkboxes render sensible defaults out of the box, but you can subclass them to match your design system. Override view_template to change the default markup — the block form still works for one-off customizations.
…Render it with ``. For file uploads, pass enctype: "multipart/form-data" to the form constructor.
The best part? If you have forms with a completely different look and feel, you can extend the forms just like you would a Ruby class:
class AdminForm
Benefits
# Superform — one-liner
Field(:benefit_ids).checkboxes(Benefit.select(:id, :name))
# Or with custom markup
fieldset do
legend { "Benefits" }
Field(:benefit_ids).checkboxes(Benefit.select(:id, :name)) do |choice|
choice.label {
choice.input
whitespace
plain choice.text
}
end
endSuperform handles the field name (benefit_ids[]), checked state, unique ids, and label targeting automatically. The same pattern works for radio groups with radios(...).
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.
I built some pretty amazing applications with Simple Form and admire its syntax. It requires "Erb soup", which is an opening and closing line of Erb per line. If you follow a specific directory structure or use their component framework, you can get pretty far, but you'll hit a wall when you need to start putting wrappers around forms or inputs.
https://github.com/heartcombo/simple_form#the-wrappers-api
The API is there, but when you change the syntax, you have to reboot the server to see the changes. UI development should be reflected immediately when the page is reloaded, which is what Superforms can do.
Like Rails form helpers, it doesn't self-permit parameters.
https://www.ruby-toolbox.com/projects/simple_form
Formtastic gives us a nice DSL inside of Erb that we can use to create forms, but like Simple Form, there's a lot of opening and closing Erb tags that make the syntax clunky.
It has generators that give you Ruby objects that represent HTML form inputs that you can customize, but its limited to very specific parts of the HTML components. Superform lets you customize every aspect of the HTML in your form elements.
It also does not permit its own parameters.
https://www.ruby-toolbox.com/projects/formtastic
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To preview example forms in your browser, run:
$ bin/previewThis boots a local Rails server at http://localhost:3000 that displays all forms in the examples/ directory. The server supports hot-reloading, so you can edit forms and refresh the page to see changes.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/rubymonolith/superform. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Superform project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
暂无开放 Issues,或尚未同步最近议题。