Use this gem to generate HTML forms using a DSL. This DSL follows the guidelines from the project "Form Generator," which is a part of the Ruby-on-Rails course offered by Hexlet (in the Russian language only, currently).
Add this line to your application's Gemfile:
gem 'hexlet_code', git: 'https://github.com/yurivyatkin/rails-project-lvl1'
And then execute:
$ bundle install
After installing this gem, your code will have access to the HexletCode
class, which provides the method form_for
for generating HTML code.
The method form_for
expects at least one mandatory argument, which should be a Ruby object with fields of primitive types (currently, we support only strings).
Here is a basic example.
User = Struct.new(:name, :job, :gender, keyword_init: true)
user = User.new name: 'rob', job: 'hexlet', gender: 'm'
HexletCode.form_for user do |f|
f.input :name
f.input :job, as: :text
end
It will generate the following HTML code:
<form action="#" method="post"><label for="name">Name</label><input name="name" type="text" value="rob"><textarea name="job" cols="20" rows="40">hexlet</textarea></form>
Fields can have additional attributes in a hash as the last parameter:
HexletCode.form_for user, url: '#' do |f|
f.input :name, class: 'user-input'
f.input :job
end
It generated the following HTML code:
<form action="#" method="post"><label for="name">Name</label><input name="name" type="text" value="rob" class="user-input"><label for="job">Job</label><input name="job" type="text" value=""></form>
Fields may have default values, which can be overridden. For example, this code
HexletCode.form_for user do |f|
f.input :job, as: :text
end
will generate HTLM with the default values like so:
<form action="#" method="post"><textarea name="job" cols="20" rows="40">hexlet</textarea></form>
whereas specifying the values for the cols
and rows
attributes,
HexletCode.form_for user, url: '#' do |f|
f.input :job, as: :text, rows: 50, cols: 50
end
we get the customized HTML snippet:
<form action="#" method="post"><textarea cols="50" rows="50" name="job">hexlet</textarea></form>
When the input
method receives a field, which is missing from the base object (the first argument of the form_for
method), a NameError
will be raised.
Using the method submit
, one can add a submit button to the form, implemented as input of type "submit". The default title of the button (i.e., the value of the tag submit
) is "Save," but passing a string parameter to the method submit
will override this value. The code f.submit
will generate <input type="submit" value="Save">
, whereas the code f.submit 'Wow'
results in <input type="submit" value="Wow">
.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
Run bundle exec rake install to install this gem onto your local machine. 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/yurivyatkin/rails-project-lvl1.