rails check_box_tag set checked with default value

后端 未结 6 1392
说谎
说谎 2021-02-18 14:38

I currently have a rails check_box_tag call that looks like

check_box_tag #{name}

I want to include a checked attribute, which I know I can do

6条回答
  •  北海茫月
    2021-02-18 15:09

    There are no ways to do it directly. But the check_box_tag implementation is trivial, you can monkey patch it or create own helper.

    Original implementation:

      def check_box_tag(name, value = "1", checked = false, options = {})
        html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
        html_options["checked"] = "checked" if checked
        tag :input, html_options
      end
    

提交回复
热议问题