How do you handle sending back files from turbo_stream?
respond_to do |format|
format.turbo_stream{ send_data service.xls_string, filename: service.file_name, type: "application/vnd.ms-excel" }
end
How do you handle sending back files from turbo_stream?
respond_to do |format|
format.turbo_stream{ send_data service.xls_string, filename: service.file_name, type: "application/vnd.ms-excel" }
end
I’m not sure this is possible. What are you trying to do?
We have our legacy code which opens modal and submit form to download generated XLS file.
Now I have universal (bootstrap) modal which is driven by Turbo frame and stream which works so far ok but not for the send_request.
To cleanup the view code I have helpers to render things.
def link_to_modal(name = nil, options = nil, html_options = nil, &block)
default_options = {data: {"turbo-frame": :modal_containers}}.deep_merge(options || {})
link_to(name, default_options, html_options, &block)
end
def render_turbo_modal(id, label:, format: nil, turbo: {}, modal: {}, &block)
formats = {html: "text/html", turbo_stream: "text/vnd.turbo-stream.html"}
request_format = formats.fetch(format, nil) || request.format.to_s
case request_format
when "text/html"
turbo_frame_tag(:modal_containers) { turbo_modal_frame(id, label: label, turbo: turbo, modal: modal, &block) }
when "text/vnd.turbo-stream.html"
turbo_stream.update(:modal_containers) { turbo_modal_frame(id, label: label, turbo: turbo, modal: modal, &block) }
end
end
def turbo_modal_frame(id, label:, turbo: {}, modal: {}, &block)
turbo_defaults = {id: id}.merge(turbo)
modal_defaults = {title: label, id: "#{id}_modal", size: nil,
data: {controller: "application--modal", auto_open: true}}.deep_merge(modal)
render("application/modal_frame", turbo: turbo_defaults, modal: modal_defaults, &block)
end