【问题标题】:Remove unwanted div from any generated from authenticity_token form从authenticity_token表单生成的任何内容中删除不需要的div
【发布时间】:2011-01-28 20:43:06
【问题描述】:

这应该很容易,但我找不到答案! 我的 Rail Forms 生成了一个我想去掉的 div

这是 rails 为我生成的 div

   <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>

我查看了一些建议我应该使用form_authenticity_token的预览问题

我可以在代码中的何处以及如何使用 form_authenticity_token 代替?

【问题讨论】:

    标签: ruby-on-rails forms actionpack


    【解决方案1】:

    您使用的是什么版本的 Rails?

    我不知道你为什么要这样做。如果是 CSS 问题,您可以更具体一些。我从来不需要这样做。 不过……

    在 3.0.9 中执行此操作的方法是创建一个初始化程序并添加以下代码:

    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
    

    【讨论】:

    • 我也遇到过类似的 CSS 问题。我想使用 :first-child 伪选择器。那是行不通的,因为第一个孩子总是隐藏的 div。
    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多