搜尋

首頁  >  問答  >  主體

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
伊谢尔伦伊谢尔伦2777 天前486

全部回覆(0)我來回復

無回覆
  • 取消回覆