lib/hanami/helpers/form_helper.rb in hanami-helpers-1.3.1 vs lib/hanami/helpers/form_helper.rb in hanami-helpers-1.3.2

- old
+ new

@@ -228,10 +228,11 @@ # @param name [Symbol] the toplevel name of the form, it's used to generate # input names, ids, and to lookup params to fill values. # @param url [String] the form action URL # @param options [Hash] HTML attributes to pass to the form tag and form values # @option options [Hash] :values An optional payload of objects to pass + # @option options [Hash] :params An optional override of params # @param blk [Proc] A block that describes the contents of the form # # @overload form_for(form, attributes = {}, &blk) # Use Form # @param form [Hanami::Helpers::FormHelper::Form] a form object @@ -407,22 +408,41 @@ # <input type="text" name="delivery[customer_name]" id="delivery-customer-name" value=""> # <input type="text" name="delivery[address][city]" id="delivery-address-city" value=""> # # <button type="submit">Create</button> # </form> - def form_for(name, url = nil, options = {}, &blk) + # + # @example Override params + # <%= + # form_for :song, routes.songs_path, params: { song: { title: "Envision" } } do + # text_field :title + # + # submit 'Create' + # end + # %> + # + # <!-- output --> + # + # <form action="/songs" accept-charset="utf-8" id="song-form" method="POST"> + # <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d"> + # <input type="text" name="song[title]" id="song-title" value="Envision"> + # + # <button type="submit">Create</button> + # </form> + def form_for(name, url = nil, options = {}, &blk) # rubocop:disable Metrics/MethodLength form = if name.is_a?(Form) options = url || {} name else Form.new(name, url, options.delete(:values)) end + params = options.delete(:params) opts = options.dup opts[:"data-remote"] = opts.delete(:remote) if opts.key?(:remote) attributes = { action: form.url, method: form.verb, 'accept-charset': DEFAULT_CHARSET, id: "#{form.name}-form" }.merge(opts) - FormBuilder.new(form, attributes, self, &blk) + FormBuilder.new(form, attributes, self, params, &blk) end # Returns CSRF Protection Token stored in session. # # It returns <tt>nil</tt> if sessions aren't enabled or the value is missing.