cari

Rumah  >  Soal Jawab  >  teks badan

ruby - 如何更好的理解form_for的源码?

我注意到最后还是调用的form_tag,中间代码部分不是很清楚做什么。学习下源码有助于更好地使用rails吧。

      def form_for(record, options = {}, &block)
        raise ArgumentError, "Missing block" unless block_given?
        html_options = options[:html] ||= {}

        case record
        when String, Symbol
          object_name = record
          object      = nil
        else
          object      = record.is_a?(Array) ? record.last : record
          raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
          object_name = options[:as] || model_name_from_record_or_class(object).param_key
          apply_form_for_options!(record, object, options)
        end

        html_options[:data]   = options.delete(:data)   if options.has_key?(:data)
        html_options[:remote] = options.delete(:remote) if options.has_key?(:remote)
        html_options[:method] = options.delete(:method) if options.has_key?(:method)
        html_options[:authenticity_token] = options.delete(:authenticity_token)

        builder = instantiate_builder(object_name, object, options)
        output  = capture(builder, &block)
        html_options[:multipart] ||= builder.multipart?

        form_tag(options[:url] || {}, html_options) { output }
      end
伊谢尔伦伊谢尔伦2774 hari yang lalu485

membalas semua(0)saya akan balas

Tiada jawapan
  • Batalbalas