I’m creating a new Hanami application using Bootstrap. When dealing with forms, I have a lot of fragments like this:
form_for :link, routes.link_path do
div class: 'container' do
div class: 'form-group row' do
label :url, class: 'col-sm-2 col-form-label'
div class: 'col-sm-10' do
text_field :url, class: 'form-control'
end
end
div class: 'form-group row' do
label :title, class: 'col-sm-2 col-form-label'
div class: 'col-sm-10' do
text_field :title, class: 'form-control'
end
end
div class: 'form-group row' do
label :lead, class: 'col-sm-2 col-form-label'
div class: 'col-sm-10' do
text_area :lead, class: 'form-control'
end
end
end
end
Since it’s a lot of unnecessary repetition, I would like to abstract out part of it and have, say:
form_for :link, routes.link_path do
div class: 'container' do
item(:text_field, :url)
item(:text_field, :title)
item(:text_area, :lead)
end
end
Tried with:
module Web::Views
module Forms
include Hanami::Helpers
def item(type, name)
div class: 'form-group row' do
label name, class: 'col-sm-2 col-form-label'
div class: 'col-sm-10' do
text_field name
public_send(type, name, class: 'form-control')
end
end
end
end
end
… but it does not work. Any ideas how to approach this? Is that possible to achieve something like that?
PS. By “not work” I mean:
ArgumentError: wrong number of arguments (given 2, expected 1)
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/rendering.rb:198:in `method_missing'
/Users/pawel/dev/priv/baiter/apps/web/views/forms.rb:6:in `item'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/rendering/scope.rb:69:in `method_missing'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:383:in `method_missing'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:383:in `method_missing'
/Users/pawel/dev/priv/baiter/apps/web/templates/link/new.html.erb:5:in `block (3 levels) in singleton class'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:364:in `instance_exec'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:364:in `resolve'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_node.rb:58:in `content'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/form_helper/html_node.rb:49:in `content'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_node.rb:44:in `to_s'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:332:in `map'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:332:in `to_s'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/form_helper/form_builder.rb:140:in `to_s'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_node.rb:61:in `content'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/form_helper/html_node.rb:49:in `content'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_node.rb:44:in `to_s'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:332:in `map'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/html_helper/html_builder.rb:332:in `to_s'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-helpers-1.0.0/lib/hanami/helpers/form_helper/form_builder.rb:140:in `to_s'
/Users/pawel/dev/priv/baiter/apps/web/templates/link/new.html.erb:35:in `block in singleton class'
/Users/pawel/dev/priv/baiter/apps/web/templates/link/new.html.erb:-6:in `instance_eval'
/Users/pawel/dev/priv/baiter/apps/web/templates/link/new.html.erb:-6:in `singleton class'
/Users/pawel/dev/priv/baiter/apps/web/templates/link/new.html.erb:-8:in `__tilt_70145548759920'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.7/lib/tilt/template.rb:170:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.7/lib/tilt/template.rb:170:in `evaluate'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.7/lib/tilt/template.rb:109:in `render'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/template.rb:41:in `render'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/rendering.rb:139:in `rendered'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/rendering.rb:153:in `layout'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/rendering.rb:107:in `render'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-view-1.0.0/lib/hanami/view/rendering.rb:258:in `render'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/rendering_policy.rb:56:in `_render_action'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/rendering_policy.rb:48:in `_render'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/rendering_policy.rb:38:in `render'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/application.rb:169:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/2.3.0/delegate.rb:83:in `method_missing'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/http_router-0.11.2/lib/http_router.rb:193:in `process_destination_path'
(eval):15:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/http_router-0.11.2/lib/http_router.rb:288:in `raw_call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-router-1.0.0/lib/hanami/routing/http_router.rb:156:in `raw_call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/http_router-0.11.2/lib/http_router.rb:142:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-router-1.0.0/lib/hanami/router.rb:1016:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:22:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/assets/static.rb:49:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/content_length.rb:15:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/common_logger.rb:33:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/builder.rb:153:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/app.rb:42:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shotgun-0.9.2/lib/shotgun/loader.rb:86:in `proceed_as_child'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shotgun-0.9.2/lib/shotgun/loader.rb:31:in `call!'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shotgun-0.9.2/lib/shotgun/loader.rb:18:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/hanami-1.0.0/lib/hanami/assets/static.rb:49:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/lint.rb:49:in `_call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/lint.rb:37:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/show_exceptions.rb:23:in `call'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/handler/webrick.rb:86:in `service'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/Users/pawel/.rbenv/versions/2.3.1/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'