Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uploadcare/rails/action_view/uploadcare_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#uploadcare_file(method, ctx_name: nil, solution: "regular", **options) ⇒ ActiveSupport::SafeBuffer

Generates an Uploadcare File Uploader field bound to the form’s model. Automatically handles validation error wrapping.

Examples:

Basic usage

<%= f.uploadcare_file :avatar %>

With options

<%= f.uploadcare_file :document, multiple: true, accept: "application/pdf" %>

With inline solution

<%= f.uploadcare_file :photos, solution: "inline" %>

Parameters:

  • method (Symbol)

    the attribute name on the model

  • ctx_name (String, nil) (defaults to: nil)

    optional context name for linking uploader components

  • solution (String) (defaults to: "regular")

    uploader solution type: “regular”, “inline”, or “minimal”

  • options (Hash)

    additional options passed to uploadcare_config_tag

Returns:

  • (ActiveSupport::SafeBuffer)

    the rendered uploader HTML



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/uploadcare/rails/action_view/uploadcare_form_builder.rb', line 37

def uploadcare_file(method, ctx_name: nil, solution: "regular", **options)
  # Auto-detect multiple from mount_uploadcare_file_group unless explicitly set
  unless options.key?(:multiple)
    model = object_name.to_s.camelize.safe_constantize
    if model
      checker = "has_uploadcare_file_group_for_#{method}?"
      options[:multiple] = true if model.respond_to?(checker) && model.public_send(checker)
    end
  end

  ctx_name ||= SecureRandom.uuid
  field_name = "#{object_name}[#{method}]"

  # Generate the uploader field using the template's helper
  field_html = @template.uploadcare_uploader_field_tag(
    field_name,
    ctx_name: ctx_name,
    solution: solution,
    **options
  )

  # Apply error wrapping if object has validation errors on this method
  if object && uploadcare_object_has_errors?(method)
    uploadcare_error_wrapping(field_html)
  else
    field_html
  end
end