Rails How to respond to CSV and XML file types?

I have this do_export method, but when I try to access it with URL + file_name.xml or .csv I am always going to the redirect which is format.all

Also I needed to do it like this

format.xml do
      request.format = :xml
send_data (render_to_string(template: "admin/languages/do_export"))

because I am getting an error 500 when request.format is not declared. below is the whole function:

def do_export
  @language = Apollo::Language.find(params[:id])
  respond_to do |format|
    format.xml do
      request.format = :xml
      send_data (render_to_string(template: "admin/languages/do_export"))
    end
    format.csv do
      request.format = :csv
      send_data @language.as_csv
    end
    format.all { redirect_to admin_languages_path, alert: t("Invalid file type.")}
  end
end

I am using ruby 2.3.0