Forum: Ruby on Rails button_to generates DIV

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-11-17 14:50
(Received via mailing list)
Hi,

I just noticed that button_to generates a div inside the form element.
Why?

Jeroen
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 listbox (Guest)
on 2005-11-17 22:50
(Received via mailing list)
On 17-nov-2005, at 14:48, Jeroen Houben wrote:

> Hi,
>
> I just noticed that button_to generates a div inside the form
> element. Why?

Because it's a requirement, per XHTML 1.1, to have an enclosing block
element as an immediate child of the FORM element.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-11-18 00:09
(Received via mailing list)
Julian 'Julik' Tarkhanov wrote:
> element as an immediate child of the FORM element.
Wow. I wonder why they did that.. I guess div is a good choice then.

It is a bit of a bummer that we can't specify say a template for these
things, I mean the div is hardcoded, you can only change it if you
overwrite the button_to method altogether. Are there plans for something
like this?

Jeroen
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2006-12-22 01:37
hey guys, to fix this, i just hacked the button_to and url_for helpers
and included it in my application.rb:


module ActionView
  module Helpers
    module UrlHelper
      def button_to(name, options = {}, html_options = nil)

        html_options = (html_options || {}).stringify_keys
        convert_boolean_attributes!(html_options, %w( disabled ))

        if confirm = html_options.delete("confirm")
          html_options["onclick"] = "return
#{confirm_javascript_function(confirm)};"
        end
        options = {:only_path=>false}.update(options)
        url = options.is_a?(String) ? options : url_for(options)
        name ||= url

        html_options.symbolize_keys!
        tag(:input, html_options.merge({
          :type => "button", :value => name,
          :onclick => (html_options[:onclick] ?
"#{html_options[:onclick]}; " : "") +
"window.location.href='#{url_for(options)}';"
        }))

      end


      def url_for(options = {}, *parameters_for_method_reference)
        if options.kind_of? Hash
          options = { :only_path => true
}.update(options.symbolize_keys)
          escape = options.key?(:escape) ? options.delete(:escape) :
true
        else
          escape = true
        end
        url = @controller.send(:url_for, options,
*parameters_for_method_reference)
        escape ? html_escape(url).gsub(/&/,"&") : url   # replaces
the '&' with its original &
      end

    end
  end
end
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2006-12-22 14:03
(Received via mailing list)
You might be interested in this snippet of code I saw somewhere and use
in
all my Rails apps.

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  "<span class=\"error\">#{html_tag}</span>"
end

That fieldWithError div [a block element] around bad form fields can
really
wreck a layout sometimes. An aptly named span works just as well.

RSL
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2006-12-22 14:05
(Received via mailing list)
Ugh... That code should read:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  "<span class=\"error\">#{html_tag}</span>"
end

RSL
8b22ad683842861ba91f68d5d32954db?d=identicon&s=25 Jason Norris (Guest)
on 2006-12-22 14:15
(Received via mailing list)
Excellent. I just had that wreck a layout yesterday. Including this code
should be easier and cleaner than hacking up the CSS.
1263ae66d745b45e8efb88dd668a7ce0?d=identicon&s=25 YodaYid (Guest)
on 2007-07-09 21:31
(Received via mailing list)
This is a pet peeve of mine in Rails - there are a few places where
these div's pop up inappropriately IMHO.  Any idea why?

On Dec 21 2006, 8:37 pm, Aryk Grosz <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.