Search code examples
ruby-on-railsformsactionpack

Remove unwanted div from any generated from authenticity_token form


This should be an easy one but I just cant find the answer! My Rail Forms generate a div that I want to get rid of

This is the div that rails generate for me

   <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713"/><input name="authenticity_token" type="hidden" value="Z6UAdFYt3v8d1lx4BNXq5td3OMJ223i+ruKM8Ldb+5s=" /></div>

I had a look to some previews questions that were suggesting I should use form_authenticity_token

Where and how in the code can I use form_authenticity_token instead?


Solution

  • What version of Rails are you using?

    I don't know why you want to do this. If it's a CSS issue you can just be more specific. I've never needed to do this. However…

    The way to do it in 3.0.9 would be making an initializer and adding this code:

    module ActionView
      module Helpers
        module FormHelper
            def extra_tags_for_form(html_options)
              snowman_tag = tag(:input, :type => "hidden",
                                :name => "utf8", :value => "&#x2713;".html_safe)
    
              method = html_options.delete("method").to_s
    
              method_tag = case method
                when /^get$/i # must be case-insensitive, but can't use downcase as might be nil
                  html_options["method"] = "get"
                  ''
                when /^post$/i, "", nil
                  html_options["method"] = "post"
                  token_tag
                else
                  html_options["method"] = "post"
                  tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag
              end
    
              tags = snowman_tag << method_tag
              content_tag(:span, tags, :style => 'margin:0;padding:0;display:inline')
            end
      end
    end